VueRouter改變頁面標題

pengjielee發表於2019-10-24

VueRouter改變頁面標題

方法1:

const router = new Router({
  mode: "history",
  base: "/",
  routes: [
    {
      path: "/",
      name: "home",
      component: Home,
      meta: { title: "首頁" }
    },
    {
      path: "/login",
      name: "login",
      component: UserLogin,
      meta: { title: "登入" }
    },
    {
      path: "/tags",
      name: "tags",
      component: Tags,
      meta: { title: "標籤頁" }
    }
  ]
});

router.afterEach(to => {
  document.title = to.meta.title || "site title"
});
複製程式碼

方法2:

// root component
export default {
  name: "app",
  watch: {
    $route(to, from) {
        document.title = to.meta.title || "site title";
    },
  }
};
複製程式碼

相關文章