Vue-cliベースのVue単一ページプロジェクト構築の詳細

4956 ワード

vueを使用してプロジェクトを構築する場合、通常はvue足場ツールvue-cliを使用してプロジェクトを構築します.プロジェクトの構築が完了した後、jquery、element-ui、グローバルプロファイルなどの一般的なプラグインをインストールする必要があります.具体的な操作は以下の通りです.
1.足場ツールvue-cliでvueプロジェクトテンプレートをダウンロードする
// vue init        
vue init webpack myvuepjt

2.インストール項目依存
npm install

3.プロジェクトを起動し、プロジェクトが正常に運行しているかどうかを検査する
上記の手順を完了すると、vueプロジェクトが正常に有効になります.次にjquery、element-uiをインストールします.
1)jqueryのインストール
1.   jquery  
npm install jquery --save
2.   
  webpack.base.conf.js   
  ① const webpack = require("webpack")
  ② module.exports      
  plugins: [
    new webpack.optimize.CommonsChunkPlugin('common.js'),
    new webpack.ProvidePlugin({
      jQuery: "jquery",
      $: "jquery"
    })
  ]
  ③       main.js   
  import $ from 'jquery'
  Vue.prototype.$ = $
      

webpack.base.conf.js:
'use strict'
const path = require('path')
const utils = require('./utils')
const config = require('../config')
const vueLoaderConfig = require('./vue-loader.conf')
const webpack = require("webpack")

function resolve (dir) {
  return path.join(__dirname, '..', dir)
}

module.exports = {
  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'],
    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'
  },
  plugins: [
    new webpack.optimize.CommonsChunkPlugin('common.js'),
    new webpack.ProvidePlugin({
      jQuery: "jquery",
      $: "jquery"
    })
  ]
}

2)element-uiのインストール
1. npm    
npm install element-ui --save-dev
2.   element-ui
  main.js   
        
  import Vue from 'vue'
  import ElementUI from 'element-ui'
  import 'element-ui/lib/theme-chalk/index.css'
  Vue.use(ElementUI)
            
  http://element.eleme.io/#/zh-CN/component/quickstart

グローバルファイルの導入:
1)要求インタフェースファイルurlconfig.js
'use strict'
export default {
  requrl: function () {
    var baseurl = 'request_baseurl'
    return {
      url: {
        name: '  ',
        url: baseurl + 'req'
      },
      
    }
  }
}

メールでjs中
//     url    
import RequestApi from '../config/urlconfig.js'
Vue.prototype.reqpath = RequestApi.requrl()

プロジェクトのどこでも通過できます:this.reqpath.url.url導入要求リンク
2)グローバルメソッドの導入global.js
const getCookie = function (name) {
  var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)")
  if (arr = document.cookie.match(reg)) {
    return unescape(arr[2])
  } else
    return null
}
const nowtoken = getCookie("token")
const rcitlang = getCookie("rcit_environment")
export default {
  nowtoken,
  rcitlang
}

メールでjs中
import methods from './assets/js/global.js'
Vue.prototype.methods = methods

プロジェクトのどこでも、thisを通過できます.methods呼び出し方法
これで、vue-cliに基づいてvueプロジェクトを構築するために必要な一般的な構成が完了しました.本文の結び.