有时,咱们在项目中会有这样一个需求,即实现 一个侧导航栏,点击不一样的菜单项,右边内容会跟着变化,而页面手动刷新后想要使菜单激活状态保持,那么这个功能该如何实现呢?html
如今给出如下解决办法: 即加上这样一段代码便可:vue
:default-active="this.$route.path"
想要实现路由的刷新,官方并无给出解决办法,经过本身摸索和借鉴,得出了如下解决方法:this
首先,新建一个空白页面redirect.vue,而后写入这样一段代码:spa
<script> export default { beforeCreate() { console.log(this.$route) const nextPath = this.$route.query.nextPath this.$router.replace({ path: nextPath}) console.log("调用") console.log(nextPath) }, render: function(h) { return h() // avoid warning message } } </script>
以后在导航页加入一个方法,以下:code
//实现路由的局部刷新 reloadRouter(path) { this.$router.replace({ path: "/redirect", query: { nextPath: path } }); }
再经过给每个菜单项添加点击事件,便可实现该功能: router
原文出处:https://www.cnblogs.com/ktddcn/p/11354450.htmlhtm