1,配置接口地址
修改项目的接口地址,直接更改以下文件(重点关注module.exports):
/config/dev.env.js —- 开发环境接口地址
1 2 3 4 5 6 7 8 |
'use strict' const merge = require('webpack-merge') const prodEnv = require('./prod.env') module.exports = merge(prodEnv, { NODE_ENV: '"development"', API_ROOT: '"https://devapi.mkboke.com"' }) |
/config/prod.env.js —- 生产环境接口地址
1 2 3 4 5 |
'use strict' module.exports = { NODE_ENV: '"production"', API_ROOT: '"https://api.mkboke.com"' } |
然后我项目ajax请求用的axios插件,我配置了axios请求拦截和响应拦截,在里面要加上一行(axios.defaults.baseURL = process.env.API_ROOT)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
import axios from 'axios' import { Message } from 'element-ui' import router from './router' // 打包项目请求不同的接口地址 axios.defaults.baseURL = process.env.API_ROOT // 请求拦截 axios.interceptors.request.use(config => { // 加载动画 // startLoading() if (localStorage.mkSellerToken) { // 设置统一的请求header config.headers.Authorization = localStorage.mkSellerToken } return config }, error => { // 错误提醒 return Promise.reject(error) }) // 响应拦截 axios.interceptors.response.use((response) => { // 结束加载动画 // endLoading() return response }, error => { // endLoading() Message.error(error.response.data) // 获取错误状态码 const { status } = error.response if (status === 401) { Message.error('会话已过期,请重新登录!') // 清除Token localStorage.removeItem('mkSellerToken') // 跳转登录页面 router.push('/login') } return Promise.reject(error.response) }) export default axios |
2,打包目录更改(npm run build)
/config/index.js 默认是打包到dist目录,我的项目改成view目录
1 2 3 4 5 6 7 |
// Template for index.html index: path.resolve(__dirname, '../view/index.html'), // Paths assetsRoot: path.resolve(__dirname, '../view'), assetsSubDirectory: 'static', assetsPublicPath: '/', |
3,路由守卫
大部分页面需要登录才能访问,未登录访问页面需要跳到登录页面,这个必须要借助vue-router插件的路由功能,下面是我整个router的代码,重点从第200行开始
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
import Vue from 'vue' import Router from 'vue-router' // 登录注册 import Login from '@/pages/login/Login' import Register from '@/pages/login/Register' import ForgotPassword from '@/pages/login/ForgotPassword' import Home from '@/pages/home/Home' // 订单管理 import OrderList from '@/pages/order/OrderList' import OrderDetail from '@/pages/order/OrderDetail' import MassShip from '@/pages/order/MassShip' import MassPrint from '@/pages/order/MassPrint' import ExcelShip from '@/pages/order/ExcelShip' // 商品管理 import ProductList from '@/pages/product/ProductList' import ProductEdit from '@/pages/product/ProductEdit' import ShopCategory from '@/pages/product/ShopCategory' import SpecList from '@/pages/product/SpecList' import ProductReviews from '@/pages/product/ProductReviews' import BrandList from '@/pages/product/BrandList' import AgentProduct from '@/pages/product/AgentProduct' import AddAgentProduct from '@/pages/product/AddAgentProduct' // 活动管理 import FlashSale from '@/pages/activity/FlashSale' import FlashSaleEdit from '@/pages/activity/FlashSaleEdit' import RecommendSale from '@/pages/activity/RecommendSale' // 员工管理 import RoleList from '@/pages/account/RoleList' import AccountList from '@/pages/account/AccountList' import RecordList from '@/pages/account/RecordList' // 店铺管理 import ShopInformation from '@/pages/store/ShopInformation' import ServiceMange from '@/pages/store/ServiceMange' import ShippingTemplate from '@/pages/store/ShippingTemplate' import TemplateEdit from '@/pages/store/TemplateEdit' // 帮助中心 import Help from '@/pages/help/Help' import HelpCategory from '@/pages/help/HelpCategory' import HelpDetail from '@/pages/help/HelpDetail' Vue.use(Router) const router = new Router({ routes: [ { path: '/login', name: 'Login', component: Login }, { path: '/register', name: 'Register', component: Register }, { path: '/forgot-password', name: 'ForgotPassword', component: ForgotPassword }, { path: '/', name: 'Home', component: Home }, { // 订单管理 path: '/order-list', name: 'OrderList', component: OrderList }, { path: '/order-detail', name: 'OrderDetail', component: OrderDetail }, { path: '/mass-ship', name: 'MassShip', component: MassShip }, { path: '/mass-print', name: 'MassPrint', component: MassPrint }, { path: '/excel-ship', name: 'ExcelShip', component: ExcelShip }, { // 商品管理 path: '/product-list', name: 'ProductList', component: ProductList }, { path: '/product-edit', name: 'ProductEdit', component: ProductEdit }, { path: '/agent-product', name: 'AgentProduct', component: AgentProduct }, { path: '/add-agent-product', name: 'AddAgentProduct', component: AddAgentProduct }, { path: '/shop-category', name: 'ShopCategory', component: ShopCategory }, { path: '/spec-list', name: 'SpecList', component: SpecList }, { path: '/product-reviews', name: 'ProductReviews', component: ProductReviews }, { path: '/brand-list', name: 'BrandList', component: BrandList }, { // 活动管理 path: '/flash-sale', name: 'FlashSale', component: FlashSale }, { path: '/flash-edit', name: 'FlashSaleEdit', component: FlashSaleEdit }, { path: '/recommend-sale', name: 'RecommendSale', component: RecommendSale }, { // 员工管理 path: '/role-list', name: 'RoleList', component: RoleList }, { path: '/account-list', name: 'AccountList', component: AccountList }, { path: '/record-list', name: 'RecordList', component: RecordList }, { // 店铺管理 path: '/shop-inforamtion', name: 'ShopInformation', component: ShopInformation }, { path: '/service-mange', name: 'ServiceMange', component: ServiceMange }, { path: '/shipping-template', name: 'ShippingTemplate', component: ShippingTemplate }, { path: '/template-edit', name: 'TemplateEdit', component: TemplateEdit }, { // 帮助中心 path: '/help', name: 'Help', component: Help }, { path: '/help-category', name: 'HelpCategory', component: HelpCategory }, { path: '/help-detail', name: 'HelpDetail', component: HelpDetail } ] }) // 路由守卫 router.beforeEach((to, from, next) => { // const isLogin = localStorage.mkToken ? true : false let isLogin if (localStorage.mkSellerToken) { isLogin = true } else { isLogin = false } if (to.path === '/login' || to.path === '/register' || to.path === '/forgot-password') { next() } else { isLogin ? next() : next('/login') } }) export default router |
欢迎留言,1小时内回复。
我已经用vue+element做过好几个大型项目,遇到过不少坑,希望能帮到有需要的人。
另外,这些博客上的视频教程真的对开发有不小的帮助,发出来的大部分我都已经看了,良心推荐