6

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 learned this from tutorial. If not then give me a correct example,Thanks!

...
const Bar={
    template:'#panels',
    data(){
        return {title:'badss'}          
    }
        };
const Foo={
    template:"#panels",
        data(){
        return {title:'hell'}
    }
        };


const router = new VueRouter({
  routes:[
      {path:'/foo',component:Foo},
      {path:'/bar',component:Bar}
  ]
});

new Vue({
    el:'#root',
    router:router,
    data:{
    stats:stats
},
methods: {
    //
},

});

function addroute(){//watch here
    router.addRoutes([{path:'/ioio',component:{template:'#panels'}}])
}
setInterval("addroute()", 2000)//watch here
...
1
  • Are you looking for setTimeout()? Commented Sep 21, 2017 at 11:02

1 Answer 1

7

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!

Sign up to request clarification or add additional context in comments.

4 Comments

Hey Ivor, i've used this method but can't seem to then reload the page and have the components persist, any ideas on that?
That highly depends on the way you have added the additional route. I would suspect that you retrieve the routes from a server. If the person has received access to this new route, you should probably change their access on that level and make sure that on reload, they get the route initially. But if this is a temporary link that depends on state (or you are working in an offline web app), if you want to persist the route, you would have to save and retrieve the routes either from localStorage or IndexedDb. If you are using Vuex, there are some options out there ro save that to localStorage.
The way i'm doing it at the moment is just say when the about component loads, add a new route to the router, then go to that new route, navigating from about => there works, but then on page refresh, poof its gone, I did try storing it in localstorage but I lose the reference to context of the object it cannot parse it.
Check this fiddle: jsfiddle.net/ivor/ptxa3cLf. When adding the route, it also pushes a route to the routes variable and puts that in the localstorage. As localstorage does not work with objects, you need to stringify when writing and parse it when retrieving. If you have any questions, let me know!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.