使用Vue-Cli3+Webpack4搭建項目

推薦使用第二種方式建立項目圖片描述css

一、 安裝前準備

Vue CLI 須要 Node.js 8.9 或更高版本 (推薦 8.11.0+)。
npm install -g @vue/cli 安裝以後,就能夠在命令行中訪問 vue 命令。html

二、 建立一個項目

兩種方式:
(1)使用命令行vue

  • 選擇要放置項目文件的路徑,打開cmd輸入
vue create 你的文件名英文
  • 用方向鍵上下去選擇配置,選好按回車

clipboard.png

等待安裝完成便可
去到項目根目錄下使用 npm run serve 就能夠將項目跑起來了!ios

(2)使用圖形化界面npm

  • 打開cmd輸入vue ui

上述命令會打開一個瀏覽器窗口,並以圖形化界面將你引導至項目建立的流程。
選擇要放置項目文件的路徑axios

clipboard.png

  • 接着我選手動配置:

下面選項看我的需求!
功能:api

clipboard.png

配置:
clipboard.png瀏覽器

history模式和hash模式是不同的!ui

  • 最後點擊建立項目等待一會便可

使用 npm run serve 就能夠將項目跑起來了圖片描述spa

  • 在項目根目錄新建 vue.config.js 配置文件 (可選)
module.exports = {
  baseUrl: '/', // 部署應用時的根路徑(默認'/'),也可用相對路徑(存在使用限制)
  outputDir: 'dist', // 運行時生成的生產環境構建文件的目錄(默認''dist'',構建以前會被清除)
  assetsDir: 'public', //放置生成的靜態資源(s、css、img、fonts)的(相對於 outputDir 的)目錄(默認'')
  indexPath: 'index.html', //指定生成的 index.html 的輸出路徑(相對於 outputDir)也能夠是一個絕對路徑。
  pages: {
    //pages 裏配置的路徑和文件名在你的文檔目錄必須存在 不然啓動服務會報錯
    index: {
      //除了 entry 以外都是可選的
      entry: 'src/main.js', // page 的入口,每一個「page」應該有一個對應的 JavaScript 入口文件
      template: 'public/index.html', // 模板來源
      filename: 'index.html', // 在 dist/index.html 的輸出
      title: 'Index Page', // 當使用 title 選項時,在 template 中使用:<title><%= htmlWebpackPlugin.options.title %></title>
      chunks: ['chunk-vendors', 'chunk-common', 'index'] // 在這個頁面中包含的塊,默認狀況下會包含,提取出來的通用 chunk 和 vendor chunk
    }
  },
  lintOnSave: true, // 是否在保存的時候檢查
  productionSourceMap: true, // 生產環境是否生成 sourceMap 文件
  css: {
    extract: true, // 是否使用css分離插件 ExtractTextPlugin
    sourceMap: false, // 開啓 CSS source maps
    loaderOptions: {}, // css預設器配置項
    modules: false // 啓用 CSS modules for all css / pre-processor files.
  },
  //反向代理
  // devServer: {
  //   // 環境配置
  //   host: '192.168.1.53',
  //   port: 8080,
  //   https: false,
  //   hotOnly: false,
  //   open: true, //配置自動啓動瀏覽器
  //   proxy: {
  //     // 配置多個代理(配置一個 proxy: 'http://localhost:4000' )
  //     // '/api': {
  //     //   target: 'http://192.168.1.248:9888',
  //     //   // target: 'http://192.168.1.4:8999',
  //     //   pathRewrite: {
  //     //     '^/api': '/api'
  //     //   }
  //     // }
  //   }
  // },
  pluginOptions: {
    // 第三方插件配置
    // ...
  }
}
  • 使用axios

    npm i axios

    在src裏面新建utils文件夾,utils文件夾裏新建axios.js

    import axios from 'axios'
       import Router from '../router'
    
    
    
    axios.interceptors.request.use(
         config => {
           return config
         },
         error => {
           return Promise.reject(error)
         }
       )
    
    
    axios.defaults.timeout = 36000000 //設置超時時間
    
    
    
    axios.interceptors.response.use(
         response => {
           // 檢測某種狀態進行重定向
           if (response.data.code === 403) {
             Router.push({
               name: 'login'
             })
           }
           return response
         },
         error => {
           return Promise.resolve(error.response)
         }
       )
    
    
    export default axios
  • 在main.js引入
import axios from './utils/axios'
Vue.prototype.$axios = axios

so easy 的啦
圖片描述

相關文章
相關標籤/搜索