我想導(dǎo)航到頁(yè)面中的特定選項(xiàng)卡
this.$router.push({ name: "AdminNotifications", params: { tab: "requests" }, })
因此在頁(yè)面內(nèi)我可以取得參數(shù)並設(shè)定選項(xiàng)卡:
mounted() { const routeTab = this.$route.params.tab; if (routeTab) this.tab = routeTab; }
如果目前頁(yè)面不是 AdminNotifications
,則有效。
但除此之外,還有一個(gè)錯(cuò)誤:
NavigationDuplicated:避免了到目前
的冗餘導(dǎo)航
那麼...有沒(méi)有辦法只設(shè)定 tab
屬性,而不需要導(dǎo)航?
謝謝
如果您已經(jīng)到達(dá)某個(gè)路線,則無(wú)法導(dǎo)航至該路線。但是,既然您已經(jīng)在那裡,您只需將 this.tab
設(shè)定為所需的值即可:
if (this.$route.name === 'AdminNotifications') { this.tab = 'requests'; } else { this.$router.push({ name: "AdminNotifications", params: { tab: "requests" }, }) }
如果負(fù)責(zé)導(dǎo)航的元件與包含 tab
的元件不同,您可以將 tab
# 參數(shù)推送到 $route
:
if (this.$route.name === 'AdminNotifications') { this.$router.replace({ params: { tab: "requests" } }); } else { this.$router.push({ name: "AdminNotifications", params: { tab: "requests" }, }) }
在頁(yè)面元件中,將mounted
中的「watcher」替換為適當(dāng)?shù)谋O(jiān)視程序,該監(jiān)視程序?qū)?code>tab 動(dòng)態(tài)設(shè)為$route.params. tab
的任何真值:
watch: { '$route.params.tab': { handler(val) { if (val) { this.tab = val; } }, immediate: true } }