Double-loop recursive matching routing table

 
 
/**
*According to the data returned by the backend, the navigation bar permission returns all the information to be displayed, so we can only match our routing table according to the returned object, remove the items that are not returned from the routing table, and then regenerate the route surface
*/
// The data here are all simulated operations, please get the 
data according to the actual situation: {
    route: [   // Our defined routing table 
        {path:"/",name:"a",hidden: true },
        {path:"/b",name:"b",hidden:true,children:[
            {path:"/b_1",name:"b_1",hidden:true},
            {path:"/b_2",name:"b_2",hidden:true},
        ]},
        {path:"/c",name:"c",hidden:true,children:[
            {path:"/c_1",name:"c_1",hidden:true},
            {path:"/c_2",name:"c_2",hidden:true},
            {path:"/c_3",name:"c_3",hidden:true},
        ]},
        {path:"/d",name:"d",hidden:true},
    ],
    permisRoute: [ // Routing table permission returned by the backend 
        {name:"a" },
        {name:"b",permission:[
            {name:"b_1"},
        ]},
        {name:"c",permission:[
            {name:"c_1"},
            {name:"c_2"},
        ]},
    ]
},
methods: {
    /**
     * [The method here should be written in the store, and then dynamically add routes in beforeEach, vue2.0 provides the addRoutes method
     * @param {[type]} route [the routing table defined in the router]
     * @param {[type]} permisRoute [backend routing table permission]
     * @return {[type]} [generate new routing table]
     */
    filterRoute: function(route,permisRoute) {
        for(let i in permisRoute){
            const name = permisRoute[i].name;
             for (let j in route){
                 // Modify hidden to match routing table 
                if (route[j].name == name){
                    route[j].hidden = false;
                    break;
                }
            }
            if(permisRoute[i].permission){
                const permisRouteChild = permisRoute[i].permission;
                const routeChild = route.filter(itme => {
                    return itme.name == name;
                })
                // Recursive routing table 
                this .filterRoute(routeChild[0 ].children,permisRouteChild)
            }
        }
        return route
    }
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324772102&siteId=291194637