Ich m?chte zu einer bestimmten Registerkarte auf der Seite navigieren
this.$router.push({ name: "AdminNotifications", params: { tab: "requests" }, })
So kann ich innerhalb der Seite die Parameter abrufen und die Registerkarte festlegen:
mounted() { const routeTab = this.$route.params.tab; if (routeTab) this.tab = routeTab; }
Gültig, wenn die aktuelle Seite nicht AdminNotifications
ist.
Aber zus?tzlich gibt es einen Fehler:
NavigationDuplicated:避免了到當(dāng)前
Redundante Navigation
Also... gibt es eine M?glichkeit, einfach das Attribut tab
ohne Navigation festzulegen?
Danke
如果您已經(jīng)到達(dá)某個路線,則無法導(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" }, }) }
在頁面組件中,將 mounted
中的“watcher”替換為適當(dāng)?shù)谋O(jiān)視程序,該監(jiān)視程序?qū)?tab
動態(tài)設(shè)置為 $route.params.tab
的任何真值:
watch: { '$route.params.tab': { handler(val) { if (val) { this.tab = val; } }, immediate: true } }