我在 Django 項(xiàng)目之上使用 Vue3 CDN,在該項(xiàng)目中添加了 VueRouter,它工作得很好,直到我決定向路由對(duì)象添加名稱。
在我的主要組件內(nèi),我嘗試在用戶每次訪問頁面時(shí)檢查路由名稱的值。
const routes = [ { path: '/', name: 'home', component: home }, { path: '/our-program' name: 'program', component: 'program' } ] const router = VueRouter.createRouter({ history: VueRouter.createWebHashHistory(), // mode: history, routes,}); const app = Vue.createApp({ data() { return {} }, computed:{ isFocus(){ return this.$route.name; } }, mounted() { console.log(this.$route); }, }); const vm = app.use(router).mount('#pages');
我在控制臺(tái)中獲取了路徑,但 $route.name 似乎無法通過。 難道沒有人知道我做錯(cuò)了什么嗎?
this.$route.name 可以從子組件顯示,而不是像我試圖做的那樣從實(shí)際的根組件顯示。