一、開發環境使用Ajax請求,報錯
網上查的資料,在config中的index.js這樣設置
proxyTable:{
'/api':{
target:'', //此處為你的API接口地址
changeOrigin:true,
pathRewrite:{
'^/api':'' //這里理解為用api代替target中的地址
}
}
}
配置完后,請求依然報錯,大致是https證書的問題
【HPM】 Error occured while trying to proxy request /xxx/ from localhost:8080 to https://xxxx.xx/xxxx/ ( DEPTH_ZERO_SELF_SIGNED_CERT) (https://nodejs.org/api/errors.html#errors_common_system_errors)
由於后台其實沒有證書,所以就增加了個配置:
proxyTable:{
'/api':{
target:'', //此處為你的API接口地址
changeOrigin:true,
pathRewrite:{
'^/api':'' //這里理解為用api代替target中的地址
},
secure:false
}
}
二、路由鈎子--beforeRouteEnter
剛開始在路由文件中是這樣配置的:
router.js
let router=new VueRouter({
mode:'history',
routes:[
{
path:'/',
component:LoginPage,
beforeRouteEnter:(to,from,next)=>{
...//這樣發現不會觸發,需在相應的組件里加鈎子函數
}
}
]
})
修改后 login.vue
export default(){
beforeRouteEnter:(to,from,next)=>{
...//在這里加
}
}
其實在vue-router的官方文檔中寫的很清楚,自己沒有注意。


三、vue中引入bootstarp
考慮使用bootstrap來進行布局,因此需要在vue項目中引入bootstrap。
1)安裝jquery,因為bootstarp中很多依賴jquery:npm install jquery --save--dev
2)安裝bootstrap: npm install bootstrap
3)配置jquery,以插件方式打包,在webpack.base.conf.js中加入以下代碼
let webpack = require('webpack')
在module.exports中加入插件相關配置
plugins: [
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery",
"windows.jQuery": "jquery"
})
]
4).在入口文件main.js中引入bootstarp
import 'bootstrap/dist/css/bootstrap.min.css' import 'bootstrap/dist/js/bootstrap.min.js'
四、路由權限控制
- 鑒權:登錄后記錄token,使用路由鈎子beforeEach,判斷是否有登陸。
- 權限控制路由顯示:動態路由,根據登錄后的用戶角色來匹配
五、打包上線--vue-router history模式的頁面返回404
使用npm run build打包好后,訪問站點,發現首頁顯示沒有問題,但是路由跳轉到某個頁面就返回404。在開發環境運行都沒有問題,上網一搜,原來是路由的history模式,如果改成hash模式是沒有問題的,原因在於使用history模式還需要后台配置支持,vue-router官方文檔中也提到了這一點。
HTML5 history模式
