search

Home  >  Q&A  >  body text

php - Problem with dynamically adding routing in vue-router

Brief description

Recently, I have been studying the issue of permission control based on RBAC under the separation of front and backend. When using vue-router to dynamically add routes, a small problem occurred. I need to ask you for help.

The general idea is as follows:

Code

 router.beforeEach(to, from, next)
    {
        ...
        let permission = JSON.parse(window.sessionStorage.getItem('permission'))
        /*permission = [...{"client_route":"/test"}...]*/
        if (permission) {
            let newRoutes = []
            permission.map((item, index) => {
                newRoutes.push({
                    path:'${item.client_route}',
                    component: '../view${item.client_route.slice(1)}.vue',
                    meta:{Auth:true}
                })
            })
            router.addRoutes(newRoutes)
        }
        ...
    }

Here comes the problem

  1. Is there any problem with the idea?

  2. There is a problem with the above code. I don’t know how to deal with the ${} in path and component. Forgive me for not learning es6 well. ╮(︶﹏︶")╭. My original intention is to load the .vue file

  3. based on the route name returned.

Thank you everyone

为情所困为情所困2861 days ago492

reply all(1)I'll reply

  • PHP中文网

    PHP中文网2017-05-16 13:09:46

    In es6, ${} is surrounded by backticks``,你的是单引号'', so the assignment fails

    reply
    0
  • Cancelreply