鍍金池/ 問答/Linux  HTML/ beforeRouteUpdate

beforeRouteUpdate

    <ul>
      <li><router-link to="/?tab=all" exact>全部</router-link></li>
      <li><router-link to="/?tab=good" exact>精華</router-link></li>
      <li><router-link to="/?tab=share" exact>分享</router-link></li>
      <li><router-link to="/2" exact>問答</router-link></li>
      <li><router-link to="/1" exact>招聘</router-link></li>
    </ul>`
  beforeRouteUpdate (to, from, next) {
    console.log(to.path);
    console.log(to.query);
    console.log("update");
    next();
  },
  watch: {
    "$route" (to, from) {
      console.log(to.path);
      console.log(to.query);
      console.log("watch");
    }
  }
  routes: [
    {
      path: '/',
      component: topics,
      props: (route) => ({ tab: route.query.tab })
    },
    {
      path: '/:id',
      component: topics,
      props: true
    },
  ]

圖片描述

為什么我用watch能對(duì)路由變化作出響應(yīng),但是beforeRouteUpdate守衛(wèi)不觸發(fā)。

回答
編輯回答
解夏

應(yīng)該是用的對(duì)方不對(duì)

文檔上有寫:
beforeRouteUpdate(2.2 新增)
在當(dāng)前路由改變,但是該組件被復(fù)用時(shí)調(diào)用
舉例來說,對(duì)于一個(gè)帶有動(dòng)態(tài)參數(shù)的路徑 /foo/:id,在 /foo/1 和 /foo/2 之間跳轉(zhuǎn)的時(shí)候,
由于會(huì)渲染同樣的 Foo 組件,因此組件實(shí)例會(huì)被復(fù)用。而這個(gè)鉤子就會(huì)在這個(gè)情況下被調(diào)用。
可以訪問組件實(shí)例 this

2018年3月28日 05:15