1. 程式人生 > >vue專案引入vux元件

vue專案引入vux元件

搭建好vue專案後,這邊主要做的手機端,綜合比較後決定引入vux元件

1.安裝vux

npm install vux --save

或者使用 yarn

yarn add vux // 安裝
yarn upgrade vux // 更新

如果你想直接從 Github 安裝,請指定 v2 分支

npm install git://github.com/airyland/vux.git#v2

 2.安裝vux-loader (vux文件中沒有明確提到要安裝,但是是需要安裝的)

npm install vux-loader --save

3.安裝安裝less-loader (可以看看package.json,如果安裝了,就不用裝了)

npm install less less-loader --save

4.開啟build下面的webpack.base.conf.js

按照vux官網上寫的來進行配置

const vuxLoader = require('vux-loader')
const webpackConfig = originalConfig // 原來的 module.exports 程式碼賦值給變數 webpackConfig

module.exports = vuxLoader.merge(webpackConfig, {
  plugins: ['vux-ui']
})

配置完webpack.base.conf.js即如下圖所示: (記得在resolve: {extensions: ['.js', '.vue', '.json','.less']里加入.less)

'use strict'
const path = require('path')
const utils = require('./utils')
const config = require('../config')
const vuxLoader = require('vux-loader')
const vueLoaderConfig = require('./vue-loader.conf')
function resolve (dir) {
  return path.join(__dirname, '..', dir)
}



const webpackConfig = {  context: path.resolve(__dirname, '../'),
  entry: {
    app: './src/main.js'
  },
  output: {
    path: config.build.assetsRoot,
    filename: '[name].js',
    publicPath: process.env.NODE_ENV === 'production'
      ? config.build.assetsPublicPath
      : config.dev.assetsPublicPath
  },
  resolve: {
    extensions: ['.js', '.vue', '.json','.less'],
    alias: {
      'vue$': 'vue/dist/vue.esm.js',
      '@': resolve('src'),
    }
  },
  module: {
    rules: [
      {
        test: /\.vue$/,
        loader: 'vue-loader',
        options: vueLoaderConfig
      },
      {
        test: /\.js$/,
        loader: 'babel-loader',
        include: [resolve('src'), resolve('test'), resolve('node_modules/webpack-dev-server/client')]
      },
      {
        test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
        loader: 'url-loader',
        options: {
          limit: 10000,
          name: utils.assetsPath('img/[name].[hash:7].[ext]')
        }
      },
      {
        test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
        loader: 'url-loader',
        options: {
          limit: 10000,
          name: utils.assetsPath('media/[name].[hash:7].[ext]')
        }
      },
      {
        test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
        loader: 'url-loader',
        options: {
          limit: 10000,
          name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
        }
      }
    ]
  }, 
  node: {
    // prevent webpack from injecting useless setImmediate polyfill because Vue
    // source contains it (although only uses it if it's native).
    setImmediate: false,
    // prevent webpack from injecting mocks to Node native modules
    // that does not make sense for the client
    dgram: 'empty',
    fs: 'empty',
    net: 'empty',
    tls: 'empty',
    child_process: 'empty'
  }
}

module.exports = vuxLoader.merge(webpackConfig, {
  options: {},
  plugins: [{
    name: 'vux-ui'
  }]
})

5.執行沒有報錯後我們就可以按照vux教程來使用了