vue初尝试问题集锦

DATE: 2017-01-16 / VIEWS: 1200

修改npm源:
  1. npm config set registry https://registry.npm.taobao.org
全局安装:
  1. npm install webpack -g
安装组件包:
  1. npm install webpack vue-template-compiler vue-loader vue-html-loader css-loader vue-style-loader vue-hot-reload-api babel-loader babel-core babel-plugin-transform-runtime babel-preset-es2015 babel-runtime@5 --save-dev
  2. npm install vue --save
  3. npm install vue-router vue-resource --save

Unexpected token import 解决办法
  1. module:{
  2. // ...
  3. loaders:[
  4. // ...
  5. {
  6. test: /\.js$/,
  7. loader: 'babel?presets=es2015',
  8. exclude: /node_modules/
  9. }
  10. ]
  11. }

Error: Cannot resolve module 解决办法
  1. module.exports = {
  2. // ...
  3. resolve: {
  4. extensions: ['', '.js', '.vue']
  5. }
  6. }

各种 vue export 报错
  1. module.exports = {
  2. // ...
  3. babel: {
  4. presets: ['es2015']
  5. }
  6. }

免去每次生成文件需要重新引用:

1. 安装插件

  1. npm install html-webpack-plugin --save-dev
2. 修改webpack.config.js
  1. var HtmlWebpackPlugin = require('html-webpack-plugin')
  2.  
  3. module.exports = {
  4. plugins: [
  5. new HtmlWebpackPlugin({
  6. filename: '../index.html',
  7. template: path.resolve(__dirname, '../app/index/index.html'),
  8. inject: true
  9. })
  10. ]
  11. }

Vue 生产环境部署
使用 Webpack 的 DefinePlugin 来指定生产环境,以便在压缩时可以让 UglifyJS 自动删除代码块内的警告语句。例如配置:
  1. var webpack = require('webpack')
  2. module.exports = {
  3. // ...
  4. plugins: [
  5. // ...
  6. new webpack.DefinePlugin({
  7. 'process.env': {
  8. NODE_ENV: '"production"'
  9. }
  10. }),
  11. new webpack.optimize.UglifyJsPlugin({
  12. compress: {
  13. warnings: false
  14. }
  15. })
  16. ]
  17. }