[Vue warn]: You are using the runtime-only build of Vue where the template compiler is not available.
问题描述:
升级脚手架 vue-cli 4.5.15 版本之后,使用 template 时报错:
[Vue warn]: 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 模式,vue 模块的 package.json 的 main 字段默认为 Runtime-only 模式,指向“dist/vue/runtime.common.js”位置。
解决办法1:
在vue.config.js文件内加上 webpack 的如下配置:
module.exports = { // webpack配置 - 简单配置方式 configureWebpack: { resolve: { alias: { "vue$": "vue/dist/vue.esm.js", //加上这一句 } } } }
也就是将
import Vue from "vue"
这行代码被解析为:
import Vue from "vue/dist.vue.esm.js"
指定了文件的位置。没有使用 main 字段默认的文件位置。
或者
可以直接把 main.js 中的
import Vue from 'vue' //改成
import Vue from "vue/dist.vue.esm.js"
解决办法2:
我们可以修改模式,指定vue项目模式为:Runtime+compiler模式
vue.config.js 中的配置文件为:
module.exports = { runtimeCompiler:true }