通过改变 URL,在不重新请求页面的情况下,更新页面视图。
1、把home分成不同的部分,登录、用户、管理等界面。在router-index.js中修改并在侧边栏导入。这里一定要加上Vue.use(VueRouter)
2、写出Aside组件,记得把collapse等值传进去,不然会没有显示。
Aside的代码:
3、在router-index.js中添加路由守卫代码,其中的next必须写,否则路由不出来。
router.beforeEach((to, from, next) => {
localStorage.setItem("currentPathName", to.name) // 设置当前的路由名称,为了在Header组件中去使用
store.commit("setPath") // 触发store的数据更新
next() // 放行路由
})
4、在header中用这种方式进行引用:
computed: {
currentPathName () {
return this.$store.state.currentPathName; //需要监听的数据
}
},
watch: {
currentPathName (newVal, oldVal) {
console.log(newVal)
}
},
使用:
// 使用首页 {{ currentPathName }}
5、安装vuex:
npm i vuex -S
6、建立store.js来使用vuex:
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const store = new Vuex.Store({
state: {
currentPathName: ''
},
mutations: {
setPath (state) {
state.currentPathName = localStorage.getItem("currentPathName")
}
}
})
export default store
7、在main.js中引入vuex
import store from './store'
new Vue({
router,
store,
render: h => h(App)
}).$mount('#app')




