How to dynamically add child routes in a vue project

  • permission control is involved in actual development
  • how to dynamically add child routes, can addRoutes achieve

  • first of all, the permission control you mentioned will definitely be involved in the actual development. Take Chestnut, for example, if you develop a mall project, if the user does not log in, can't you view the page related to the personal center? This is the simplest access control.
  • then you say dynamically add child routes, not directly to the vue-router object as you think, but like this
const User = {
  template: '<div>User</div>'
}

const router = new VueRouter({
  routes: [
    //  
    { path: '/user/:id', component: User }
  ]
})

controlled by dynamic parameters. ide/essentials/dynamic-matching.html-sharp%E5%8A%A8%E6%80%81%E8%B7%AF%E7%94%B1%E5%8C%B9%E9%85%8D" rel=" nofollow noreferrer "> from the official website


you probably want to know how to implement front-end authentication. In fact, routing tables are defined and added in real time on the status tree. Without permissions, this route is not added. Permissions and specific menu directories need to obtain data from backend APIs. You can take a look at this article
https://www.jianshu.com/p/3ea.


you can also use beforeEach and afterEach

ide/advanced/navigation-guards.html-sharp%E5%85%A8%E5%B1%80%E5%AE%88%E5%8D%AB" rel=" nofollow noreferrer "> official documents


provides you with an idea reference: you don't need to add routes dynamically at all. All routes have been added, and the display menu can be ok with the interface


.

get the configuration array of existing routes through vm.$router.options.routes. Then find the corresponding configuration item to which you want to add a child route for the array operation (suppose it is named: mainRoute).

let children = mainRoute.children || [];
children.push({
    path: xxx,
    name: xxx,
    component: xxx
});

mainRoute.children = children;
vm.$router.addRoutes([mainRoute]);
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1c24f13-33724.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1c24f13-33724.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?