1. 程式人生 > >webpack打包vue2.0專案時必現問題。

webpack打包vue2.0專案時必現問題。

[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.

(found in )

這個問題是怎麼造成的呢,找了很久找不到處理方法,上網查了也沒找到一個好的處理方案。後來去看官方文件,找到了類似的答案。

這裡寫圖片描述

這是什麼意思呢?
執行時構建不包含模板編譯器,因此不支援 template 選項,只能用 render 選項,但即使使用執行時構建,在單檔案元件中也依然可以寫模板,因為單檔案元件的模板會在構建時預編譯為 render 函式。執行時構建比獨立構建要輕量30%,只有 17.14 Kb min+gzip大小。
上面一段是官方api中的解釋。就是說,如果我們想使用template,我們不能直接在客戶端使用npm install之後的vue。此時,再去看查vue模組,新增幾行

 resolve: {
        alias: {
            'vue': 'vue/dist/vue.js'
        }
    }

再執行,沒錯ok了。

以下是我的完成的程式碼
webpack.config.babel.js

/**
 * Created by lenovo on 2017/5/8.
 */
import path from 'path';
import HtmlWebpackPlugin from 'html-webpack-plugin';
const config = {
    entry: './src/index.js',
    output: {
        filename: 'bundle.js'
, path: path.join(__dirname, 'dist') }, module: { loaders:[ { test: /\.js$/, loader: 'babel' }, { test: /\.vue$/, loader: 'vue-loader' } ] }, plugins: [ new
HtmlWebpackPlugin({ filename: 'index.html', template: './index.html', title: 'hello App' }) ], resolve: { alias: { 'vue': 'vue/dist/vue.js' } } } export default config;

package.json

{
  "name": "demo",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "dependencies": {
    "vue": "^2.3.2"
  },
  "devDependencies": {
    "babel-core": "^6.3.26",
    "babel-loader": "^6.2.0",
    "babel-preset-es2015": "^6.24.1",
    "html-webpack-plugin": "^2.28.0",
    "webpack": "^1.12.9",
    "vue-loader": "^12.0.3",
    "vue-template-compiler":"^2.3.2"
  }
}

不知道有沒有朋友遇到過這樣的問題,如果遇到了而你正好不知道怎麼解決,我想這篇文章會幫到你。