I
I
ivankalita2019-05-16 18:49:13
Vue.js
ivankalita, 2019-05-16 18:49:13

Why doesn't $router.addRoutes work?

The situation is this, when initializing the route object, there are three route objects:

var routes = [
    { name: 'a', path: '/', component: Start },
    { name: 'b', path: '/signup', component: SignUp, alias: '/sign' }
    { name: 'd', path: '*', component: NotFound }
]

Upon successful authorization of the user in the application, his personal account should be available to him at the address: '/user'. This is done by adding a new route object to the router object:
this.$router.addRoutes([{ name: 'c', path: '/user', component: User }]);

But when I try to go to this route, I fail ((namely, a redirect to the route 'd':
5cdd85c668549947825641.png
How to be yourself, Gentlemen?)
Vue.use(Router)

var routes = [
    { name: 'a', path: '/', component: Start },
    { name: 'b', path: '/signup', component: SignUp, alias: '/sign' },
    { name: 'd', path: '*', component: NotFound }
]

var router = new Router({
    mode: 'history',
    routes: routes
})

  export default {
    name: 'app',
    router: router,
    data: function() { 
      return {
        message: 'Hello'
      }
    },
    methods: {
      addNewUrl: function(mes) {
        this.$router.addRoutes([{ name: 'c', path: '/user', component: User }]);
        console.log(this.$router);  // получаю все те же самые три маршрута 'a', 'b', 'd'
    },

PS when calling the push method "on the spot" the transition is successful, but alas, when returning to the start page '/' or registering '/signup', a second transition to '/user' is impossible (((
this.$router.addRoutes([{ name: 'c', path: '/user', component: User }]);
        this.$router.push('/user');

5cdd8769690e6368369344.png

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question