Vue 2.0 Webpack“需要适当的加载器”用于.vue文件中的模板

时间:2016-11-29 18:52:04

标签: webpack vue.js vue-component vuejs2 vue-loader

我很难通过引导的vue-cli项目找出一些功能。我不想使用webpack-dev-server,因为我想使用自己的快速应用来提供index.html文件和build.js文件。所以我决定从头开始用Webpack设置Vue 2.0。

我在运行webpack

时遇到了错误
ERROR in ./src/App.vue
Module parse failed: C:\node\aaaaa\customer\src\App.vue Unexpected token (1:0)
You may need an appropriate loader to handle this file type.
| <template lang="html">
|   <div id="app">
|     <h1>This is from App.vue</h1>
 @ ./src/main.js 2:0-27

我一直收到此错误,而vue-loader的{​​{1}}并未考虑App.vue所要求的main.js中的要求。我在另一个SO答案中遇到vue-html-loader,但我猜这是旧版本的Vue。

my-project
├──src
|  ├──App.vue
|  ├──main.js
├──node_modules
├──package.json
├──webpack.config.js

App.vuemain.jsvue-cli版本保持不变。

这是我的package.json依赖项,它与原始的vue-cli依赖项不同,不安装webpack-dev-servercross-envfile-loader

"dependencies": {
  "vue": "^2.1.0"
},
"devDependencies": {
  "babel-core": "^6.0.0",
  "babel-loader": "^6.0.0",
  "babel-preset-es2015": "^6.0.0",
  "css-loader": "^0.25.0",
  "vue-loader": "^10.0.0",
  "vue-template-compiler": "^2.1.0",
  "webpack": "^2.1.0-beta.25"
}

我的webpack.config.js不包含vue-cli版本中我认为不必要的部分内容。

module.exports = {
  entry: './src/main.js',
  output: {
    path: __dirname + '/dist',
    publicPath: '/dist/',
    filename: 'build.js'
  },
  module: {
    rules: [
      {
        test: /\.vue$/,
        loader: 'vue-loader',
        include: './src',
        options: {
          // vue-loader options go here
        }
      },
      {
        test: /\.js$/,
        loader: 'babel-loader',
        include: './src',
        exclude: /node_modules/,
        query: { //https://github.com/babel/babel-loader
          presets: ['es2015']
        }
      }
    ]
  }
};

webpack.config.js项目中的vue-cli文件是:

var path = require('path')
var webpack = require('webpack')

module.exports = {
  entry: './src/main.js',
  output: {
    path: path.resolve(__dirname, './dist'),
    publicPath: '/dist/',
    filename: 'build.js'
  },
  module: {
    rules: [
      {
        test: /\.vue$/,
        loader: 'vue-loader',
        options: {
          // vue-loader options go here
        }
      },
      {
        test: /\.js$/,
        loader: 'babel-loader',
        exclude: /node_modules/
      },
      {
        test: /\.(png|jpg|gif|svg)$/,
        loader: 'file-loader',
        options: {
          name: '[name].[ext]?[hash]'
        }
      }
    ]
  },
  resolve: {
    alias: {
      'vue$': 'vue/dist/vue.common.js'
    }
  },
  devServer: {
    historyApiFallback: true,
    noInfo: true
  },
  devtool: '#eval-source-map'
}

if (process.env.NODE_ENV === 'production') {
  module.exports.devtool = '#source-map'
  // http://vue-loader.vuejs.org/en/workflow/production.html
  module.exports.plugins = (module.exports.plugins || []).concat([
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: '"production"'
      }
    }),
    new webpack.optimize.UglifyJsPlugin({
      sourceMap: true,
      compress: {
        warnings: false
      }
    }),
    new webpack.LoaderOptionsPlugin({
      minimize: true
    })
  ])
}

任何帮助将不胜感激。如果你可以指导我到一个我可以学习使用vue-cli搭建项目和express而没有webpack-dev-server的地方,那就太棒了!

0 个答案:

没有答案