有時,咱們在項目中會有這樣一個需求,即實現 一個側導航欄,點擊不一樣的菜單項,右邊內容會跟着變化,而頁面手動刷新後想要使菜單激活狀態保持,那麼這個功能該如何實現呢?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