VueJS重新加载(如果在同一路径中)

Him*_*ack 7 vue.js vue-router

我们希望,如果单击同一页面上的router-link,该页面将重新加载。

那怎么办?watch->$route如果页面保持不变,则不会调用

Som*_*iks 5

如果您的需要只是由于路由参数更改而“刷新”组件,请尝试以下操作:

// define a watcher 
watch: {
    "$route.params.id"(val) {
      // call the method which loads your initial state
      this.find();
    },
},
Run Code Online (Sandbox Code Playgroud)

其中“id”来自 /myroute/:id 和 find() 是一个常规方法。


Per*_*erp 5

选项之一是在 RouterView 上使用 key:

<RouterView :key="whenThisVarChangeRouterViewWillBeRemounted" />
Run Code Online (Sandbox Code Playgroud)

如果您不想在每次路由器视图更改时重新加载,请确保将密钥放在合理的位置,否则您可以使用:

:key="$route.fullPath"
Run Code Online (Sandbox Code Playgroud)

这将保证每次更改路径时都会重新安装。

  • 应该是“路由”而不是“路由器”。但除此之外,它的效果非常好!谢谢 (2认同)

Ale*_*oci 2

您可以使用“beforeEnter”文档。

   { 
     path: '/foo', component: Foo,
     beforeEnter: (to, from, next) => {
       /*
        todo check if to === from
        Warning!: location.reload()  completely destroy all vuejs stored states
        */
        window.location.reload()
        return next()
     }
   }
Run Code Online (Sandbox Code Playgroud)

  • 我不确定如果用户与他刚刚单击的链接位于同一页面上,是否会调用该钩子 (2认同)