使用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 的啦
图片描述

相关文章
相关标签/搜索