装常用包
- npm i element-ui
- npm install -D less-loader less
- npm i axios
- npm i vue-router
环境变量配置
- 1.在项目根目录(与README.md同级)下创建
.env.development
(.env.production
)文件配置开发环境(生产环境)文件 - 2.内部键值对中:
VUE_APP_URL=基地址
- 3.使用:
process.env.变量名(VUE_APP_开头的变量)
- 注意点:
- 1.定义好后要重新执行
npm run serve
才能使用定义的变量 - 2.它是全局变量,任何生命同期都可调用
- 3.不要在html里面使用
process.env.VUE_APP_名字
- 4.设置完成一定要重启脚 手架
脚手架设置跨域代理
- 1.在README同级目录下创建一个
vue.config.js
- 2.在vue.config.js使用下面代码
- 3.将.env.development 内的基地址替换为 /public(VUE_APP_URL=/public)
module.exports = {
publicPath: './',
devServer: {
proxy: {
'/public': {
target: 'http://127.0.0.1/xxx',
secure: false,
changeOrigin: true,
pathRewrite: {
}
}
}
}
}
main.js
import Vue from 'vue'
import App from './App.vue'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
Vue.use(ElementUI)
Vue.config.productionTip = false
import router from '@/router/index.js'
import store from '@/store/index.js'
new Vue({
router,
store,
render: h => h(App)
}).$mount('#app')
request.js
import axios from 'axios'
import { Message } from 'element-ui'
import { getLocal, removeLocal } from '@/utils/local.js'
import router from '@/router/index.js'
const _fetch = axios.create({
baseURL: process.env.VUE_APP_URL,
withCredentials: true
})
_fetch.interceptors.request.use(
function (config) {
config.headers.token = getLocal('token')
return config
},
function (error) {
return Promise.reject(error)
}
)
_fetch.interceptors.response.use(
function (response) {
if (response.data.code == 200) {
return response
} else if (response.data.code == 206) {
Message.error(response.data.message)
removeLocal('token')
router.push('/login')
return Promise.reject(response.data.message)
} else {
Message.error(response.data.message)
return Promise.reject(response.data.message)
}
},
function (error) {
return Promise.reject(error)
}
)
export default _fetch
router.js
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
const router = new VueRouter({
routes: [
{ path: '/', redirect: '/login' },
{ path: '/login', component: () => import('@/views/login/login') }
]
})
export default router