在運行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.
出現這個錯誤的主要原因是:
vue有兩種形式的代碼 compiler(模板)模式和runtime模式(運行時),vue模塊的package.json的main字段默認為runtime模式, 指向了"dist/vue.runtime.common.js"位置
如果你需要在客戶端編譯模板 (比如傳入一個字符串給 template 選項,或掛載到一個元素上並以其 DOM 內部的 HTML 作為模板),就將需要加上編譯器,即完整版
當使用 vue-loader 或 vueify 的時候,*.vue 文件內部的模板會在構建時預編譯成 JavaScript。你在最終打好的包里實際上是不需要編譯器的,所以只用運行時版本即可
簡單解決方法:
把以下代碼:
import Vue from 'vue'
改成:
import Vue from 'vue/dist/vue.esm.js'