vue全家桶安裝以及修改webpack配置新增vue項目啓動方式

1、安裝node環境(自帶npm)css

下載地址html

 

 

 

2、替換下載源vue

// 淘寶 NPM 鏡像
npm install -g cnpm --registry=https://registry.npm.taobao.org

 

3、全局安裝vue-cli腳手架node

npm install --global vue-cli

 

4、vue2.* webpack模板下載webpack

vue init webpack vue_project

 

5、安裝vuexios

npm install vuex --save

在main.js中注入vuexweb

// vuex
import Vuex from 'vuex'
Vue.use(Vuex)

 

6、安裝axiosvuex

npm install axios --save

在main.js導入並全局使用vue-cli

// axios請求
import axios from "axios"
Vue.prototype.$axios = axios

 

7、安裝elementUInpm

npm install element-ui --save

在main.js中引入elementUI

// element樣式框架
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
Vue.use(ElementUI)

8、vue項目使用sass

npm install sass-loader --save-dev
npm install node-sass --save-dev

//連寫
npm install sass-loader node-sass --save-dev

想要使用sass,須要修改build目錄下的webpack.base.conf.js文件,增長如下規則:

{
  test: /\.sass$/,
  loaders: ['style', 'css', 'sass',"scss"]
}

在組件中使用sass語法

<style lang="scss">

  $size:50px;
  html {
    font-size: $size;
  }

</style>

或者外部引用:

<style lang="scss">

  @import "./main.scss"
  // 或者
  // @import url(./main.scss);

</style>

 9、修改webpack配置,新增項目啓動方式

以 pred 爲例:

1,首先更改package.json配置

2,在build目錄下增長pred.js,內容和build.js文件相同,更改如下三處變量便可

 

3.修改build目錄下webpack.prod.conf.js配置,替換以下代碼:

'use strict'
const path = require('path')
const utils = require('./utils')
const webpack = require('webpack')
const config = require('../config')
const merge = require('webpack-merge')
const baseWebpackConfig = require('./webpack.base.conf')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin')
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')

/** 刪除內容
  const env = process.env.NODE_ENV === 'testing'
  ? require('../config/test.env')
  : require('../config/prod.env')
*/

// 替換成
  const ENV_MAP = {
    'testing': require('../config/test.env'),
    'production': require('../config/prod.env'),
    'pred': require('../config/pred.env')
  }
  const env = process.env.NODE_ENV = ENV_MAP[process.env.NODE_ENV];

4.在config目錄新增 pred.env.js文件,內容以下

'use strict'
module.exports = {
  NODE_ENV: '"pred"'
}

5.修改config目錄下的index.js,複製build對象並修改爲pred

build: {
    // Template for index.html
    index: path.resolve(__dirname, '../dist/index.html'),

    // Paths
    assetsRoot: path.resolve(__dirname, '../dist'),
    assetsSubDirectory: 'static',
    assetsPublicPath: '/',

    /**
     * Source Maps
     */

    productionSourceMap: true,
    // https://webpack.js.org/configuration/devtool/#production
    devtool: '#source-map',

    // Gzip off by default as many popular static hosts such as
    // Surge or Netlify already gzip all static assets for you.
    // Before setting to `true`, make sure to:
    // npm install --save-dev compression-webpack-plugin
    productionGzip: false,
    productionGzipExtensions: ['js', 'css'],

    // Run the build command with an extra argument to
    // View the bundle analyzer report after build finishes:
    // `npm run build --report`
    // Set to `true` or `false` to always turn it on or off
    bundleAnalyzerReport: process.env.npm_config_report
  },
  pred: {
    // Template for index.html
    index: path.resolve(__dirname, '../dist/index.html'),

    // Paths
    assetsRoot: path.resolve(__dirname, '../dist'),
    assetsSubDirectory: 'static',
    assetsPublicPath: '/',

    /**
     * Source Maps
     */

    productionSourceMap: true,
    // https://webpack.js.org/configuration/devtool/#production
    devtool: '#source-map',

    // Gzip off by default as many popular static hosts such as
    // Surge or Netlify already gzip all static assets for you.
    // Before setting to `true`, make sure to:
    // npm install --save-dev compression-webpack-plugin
    productionGzip: false,
    productionGzipExtensions: ['js', 'css'],

    // Run the build command with an extra argument to
    // View the bundle analyzer report after build finishes:
    // `npm run build --report`
    // Set to `true` or `false` to always turn it on or off
    bundleAnalyzerReport: process.env.npm_config_report
  }

6,控制檯輸入 npm run pred,看到以下內容即成功:

相關文章
相關標籤/搜索