How to use addroutes method in Vue-router?

前端 未结 1 999
慢半拍i
慢半拍i 2020-12-19 06:27

I have created a function \"addroute()\" to add routes dynamically,but it didn\'t work(the router does not changed). Is this a right way to use addRoutes method? I have lea

相关标签:
1条回答
  • 2020-12-19 07:16

    The router instance that you are trying to change is already added to the Vue instance that you have. Changing that router does not update the Vue instance. What you want to do is call the router instance that you have added to the Vue instance. A complete working example can be found at: Add routes in vue-router

    So you access the router inside a method in the Vue instance using this.$router. You then add the desired route that you want to add using the .addRoutes functionality.

    In the fiddle, you can see that I have added the ioio link in the HTML already, but it does not work yet (clicking it will result in the <span>Default</span> being added to the <router-view></router-view>). When clicking on the button, you are adding a new route. I have added the this.$router.push('/ioio'); to force an update on the <router-view></router-view> after adding the route. If you remove this line of the code, the <router-view></router-view> will display the last shown element (which is desirable in most cases), and clicking on the ioio-button again will show the newly created route.

    Hope this helps!

    0 讨论(0)
提交回复
热议问题