首页 > 其他分享 >You are using the runtime-only build of Vue where the template compiler is not available ,页面自定义带temp

You are using the runtime-only build of Vue where the template compiler is not available ,页面自定义带temp

时间:2023-08-07 22:48:28浏览次数:33  
标签:vue 自定义 component Vue 报错 template 组件 my

使用vue-cli搭建的项目,页面自定义带template内容的组件无法渲染,控制台报错,页面不展示组件内容,代码如下:

<template>
  <div class="hello">
  my-component:<my-component></my-component>
  </div>
</template>

<script>
import Vue from "vue";
Vue.component("my-component", {
  template: "<div>my-component:这是我写的一个组件!==局部组件</div>",
});

export default {
  name: 'HelloWorld',
}
</script>

 

 控制台报错: You are using the runtime-only build of Vue where the template compiler is not available. Either pre-compile the templates into render functions, or use the compiler-included build. 问题分析: 您正在使用Vue的仅运行时版本,其中模板编译器不可用。 可以将模板预编译为渲染函数,也可以使用包含编译器的内部版本 问题原因:runtime-compiler和runtime-only,在项目配置的时候,使用npm 包默认导出的是运行时构建,即 runtime-only 版本,不支持编译 template 模板。   解决方法有以下几种:
  1. 在vue.config.js文件或者webpack配置文件中添加配置,重新启动项目,刷新页面
module.exports = {
  // webpack配置 - 简单配置方式
  configureWebpack: {
    resolve: {
      alias: {
        // 别名
        vue$: "vue/dist/vue.esm.js", //加上这一句
      }
    }
  },
}
或者:
const { defineConfig } = require('@vue/cli-service')
module.exports = defineConfig({
  configureWebpack: {
    resolve: {
      alias: {
        // 别名
        vue$: "vue/dist/vue.esm.js", //加上这一句
      }
    }
  },
})

 

 
  1. 在vue.config.js文件或者webpack配置文件中添加配置,重新启动项目,刷新页面
module.exports = {
    runtimeCompiler:true   //添加这一行
}
或者:
const { defineConfig } = require('@vue/cli-service')
module.exports = defineConfig({
  runtimeCompiler: true  //添加这一行
})

 

  1. 将预编译的template放入render函数中执行
//template:
  my-component:<my-component></my-component>
  my-component2:<my-component2></my-component2>


//script:以下两种写法都可以
import Vue from "vue";
Vue.component("my-component", {
  //template: "<div>my-component:这是我写的一个组件!==局部组件</div>",
  render: function (h) {
    return h('div', 'Hello, 我是my-component')
  }
});
Vue.component("my-component2", {
  render: function (h) {
  return h('div', {
    attrs: {
      id: 'example'
    },
    style: {
      backgroundColor: 'skyblue',
      color: 'white',
      fontSize: '20px'
    }
  }, 'Hello, 我是my-component2')
}
});

 

     

标签:vue,自定义,component,Vue,报错,template,组件,my
From: https://www.cnblogs.com/jjSmileEveryDay/p/17612929.html

相关文章

  • php多维数组自定义排序 uasort()
    对数组进行排序PHP有一些用来排序数组的函数,这个文档会把它们列出来。主要区别有:有些函数基于array的键来排序,而其他的基于值来排序的:$array['key']='value';。排序之后键和值之间的关联关系是否能够保持,是指排序之后数组的键可能会被重置为数字型的(0,1,2...)。排......
  • 支持多数据源联合查询的SQL运行引擎sycnany-SQL添加使用自定义函数
    在微服务和云原生愈发流行的今天,数据的分布也愈发脱离单库单机而更加复杂,使用的数据库类型也会更多,但业务的复杂依然会带来了大量的数据查询和导出需求,而很多时候我们很难为数据量的大部分系统创建完整的BI数仓系统,这时候你是不是觉得为这些需求查询和导出数据就会是一个十分困难且......
  • AWC启动时间表交付件流程报错
    1、问题:在AWC启动时间表任务交付件流程时。启动流程不成功,报了如下错误:由“test”在节点“plmtestapp”上锁定,该节点位于由“2023-8-0717:21”开始的会话中。实例在使用中。无法加载或刷新对象“xxxxx”。请参考此错误堆栈中之前的错误以获取更多信息2、原因:除了把时间表......
  • Element UI 在非template/render 模式下使用ICON要注意的问题
    有很多时候,我们不需要编译Vue和ElementUI,只是想简单的试用一下,做一个原型出来。我们会使用HTML方式编写,这种方式下,使用ICON需要注意一些问题。1.例如CopyDocument图标,如果是用htmltag方式调用,根据vue的组件规范,驼峰格式需要改写成小写横线分割的格式<copy-document></copy-docu......
  • 【Rabbitmq】报错:ERROR CachingConnectionFactory Channel shutdown: channel error
    目录01报错原因02解决方案2.1全局配置2.2注解方式原文链接:【Rabbitmq】报错:ERRORCachingConnectionFactoryChannelshutdown:channelerror异常信息Channelshutdown:channelerror;protocolmethod:#method<channel.close>(reply-code=406,reply-text=PRECONDITI......
  • 如何自定义书写中间件?
    一、什么是中间件?中间件是一种装配到应用管道以处理请求和响应的软件。是介于request与response处理过程之间的一个插件(一道处理过程),相对比较轻量级,并且在全局上会影响到request对象和response对象的属性。因为改变的是全局,所以需要谨慎实用,用不好会影响到性能。每个组件:1、选......
  • node14 升级 node16 后 vue2 项目中 sass 报错问题
    起因不知道因为个什么手贱把之前的node14版本卸载了去官网重新下载安装了一下node,最近版本升级到了node16,以为应该不会有什么问题吧,结果把项目一跑,我勒个去,一堆飘红的,看控制台提示主要是这个node-sass报的错。  #卸载npmuninstallnode-sasssass-loader#重新安......
  • ubuntu系统升级软件sudo apt upgrade后GPU崩溃报错,显示驱动版本不匹配——ubuntu系统
     ubuntu系统升级软件(sudoaptupgrade)后,GPU崩溃报错,查看系统日志: Aug206:25:02lcwtrsyslogd:[originsoftware="rsyslogd"swVersion="8.32.0"x-pid="2059"x-info="http://www.rsyslog.com"]rsyslogdwasHUPedAug207:17:01lcwtCRON......
  • vue报错 Multiple assets emit different content to the same filename index.html
    vue-cli版本:@vue/[email protected]报错现象:想把css和script全部内嵌到html文件中,就用了"HtmlInlineScriptPlugin"插件,打包后js代码被嵌到了head里,导致代码提前执行找不到#app,再配置HtmlWebpackPlugin插件通过inject:"body"指定代码内嵌到body,打包报错"Multipleassetsemitdiff......
  • 解决报错:Redis ERR unknown command ‘FLUSHDB‘
    RedisERRunknowncommand‘FLUSHDB’报错信息:ERRunknowncommand`flushdb`ERRunknowncommand`flushall`解决方案:我的redis版本是5.0.7修改配置文件打开/etc/redis/redis.conf文件,将下面两行代码注释掉rename-commandFLUSHALL37_dba_FLUSHALLrename-commandFLUSHDB......