nuxt 设置路由的meta属性

1. 根目录新建 server文件夹,server文件夹下创建 router.js 文件

2. router.js

/**
 * nuxt的路由菜单配置
 * @description 仅仅只用于侧边栏的菜单显示和权限,其它的不做任何功能
 */
const menus = [
    {
        meta: {
            requireAuth: false, //菜单权限
            title: '列表Demo', //菜单名
        },
        path: "List",
        name: "demos-List"
    },
    {
        path: "/",
        name: "index"
    }
];


 
/**
 * 递归查询路由权限
 * @param {*} list
 * @param {*} menu
 */
const iterator = list => {
  for (let item of list) {
    for (let m of menus) {
      if (item.name === m.name && item.path === m.path) {
        item.meta = m.meta; // item.meta.requireAuth = true;
      }
    }
    if (item.children && item.children.length > 0) {
      iterator(item.children);
    }
  }
  return list;
};

export default (routes, resolve) => {
  routes = iterator(routes);
};

3. nuxt.config.js

import routes from './server/router.js';

export default{
    router: {              // customize nuxt.js router (vue-router).
    middleware: ['authorities'],  // middleware all pages of the application
    extendRoutes: routes
  },
}

 

posted @ 2021-03-04 10:49  没事看看电影  阅读(1953)  评论(0编辑  收藏  举报