我在自行使用webpack搭建单页面应用框架时,引入了vue,在对入口文件进行挂在模板时,报错以下:vue
[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.复制代码
main.js代码以下:webpack
import Vue from 'vue';import App from './App.vue';
new Vue({
el: "#app",
template: '<App/>',
component: {App}
});复制代码
下面一块儿来分析下出错的缘由和解决办法。git
vue有两种形式的代码 compiler
(模板)模式和 runtime
模式(运行时),vue模块的package.json的main字段默认为runtime模式, 指向了"dist/vue.runtime.common.js"位置。
web
而在main.js文件中,初始化vue使用的是compiler模式,因此就会出现上面的错误信息。
正则表达式
将main.js初始化vue使用runtime模式,以下:json
new Vue({
router,
store,
render: h => h(App)
}).$mount("#app")复制代码
webpack配置文件里有个别名配置,以下:
bash
resolve: {
alias: {
'vue$': 'vue/dist/vue.esm.js' //内部为正则表达式 vue结尾的
}
}复制代码
也就是说,import Vue from ‘vue’ 这行代码被解析为 import Vue from ‘vue/dist/vue.esm.js’,直接指定了文件的位置,没有使用main字段默认的文件位置。即把配置文件别名配置修改成:app
configureWebpack: {
resolve: {
alias: {
'vue$': 'vue/dist/vue.esm.js'
}
}
}复制代码
那就是在引用vue时,直接写成以下便可:
框架
import Vue from 'vue/dist/vue.esm.js'复制代码
总结,学习就是要不断采坑,同时也是在不断提高本身。学习
查看完整代码,详见个人仓库=>冲鸭!