为什么不添加$ router.addRoutes路由?

时间:2019-03-30 17:26:31

标签: vue.js vue-component vue-router

为什么不添加路由$ router.addRoutes? 我尽力了

async loadMenu() {
        try{
            let res = await this.$http.get('/menu')
            this.menuItems = res.data
            res.data.forEach((e) => {
                this.$router.addRoutes([
                    {
                        path: /${e.url},
                        name: ${e.name},
                        component: require(`${e.compURL}`)
                    },
                ])
                console.log(e)
            })
        }catch(err){
            console.log(err)
        }
    },

我不明白为什么不添加动态路由? xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

const router = new Router({
    routes: [
        {
            path: '/',
            name: 'home',
            component: Home,
            meta: {
                title: 'EHZ',
                layout: 'default-layout',
                catId: 1
            }
        },
    ],
    mode: 'history',
    // base: locale,
})

router.beforeEach((to, from, next) => {
    if (to.meta.requiresAuth) { 

            Vue.axios.post('/auth/token')
                .then((res) => {
                    if ( res.status === 200 )
                        next()
                })
                .catch((e) => {
                    store.dispatch('logout')
                    next({ path: '/auth/login'})
                })
    }
    else {
        next()
    }
})

export default router;

我添加了router.js中的代码。 还需要显示其他内容吗?


我添加了一张在 res.data

中获得的图片

image res.data

1 个答案:

答案 0 :(得分:0)

好吧,您可以按照以下步骤实现

header.vue内部文件

<template>
  <div class="hello">
    <h1>{{ msg }}</h1>
    <router-link to="/">Home</router-link>
    <div v-for="menu in dynamic" :key="menu.path">
      <router-link :to="menu.path">{{menu.name}}</router-link>
    </div>
  </div>
</template>

<script>
import About from "@/components/About";
import Contact from "@/components/Contact";
export default {
  name: "Header",
  data: function() {
    return {
      msg: "Header Component!",
      dynamic: [
        {
          name: "About",
          component: "About",
          path: "/about"
        },
        {
          name: "Contact",
          component: "Contact",
          path: "/contact"
        }
      ]
    };
  },
  created() {
    console.log("inside created");
    const routes = this.routerFormat(this.dynamic);
    console.log("routes ", routes);
    this.$router.addRoutes(routes);
  },
  methods: {
    routerFormat: function rFormat(routers) {
      console.log("inside routerFormat");
      if (!(routers instanceof Array)) {
        return false;
      }
      let fmRouters = [];
      routers.forEach(router => {
        let { path = "/404", component = "Error", name, children } = router;
        path = path || "";
        component = component || "Error";
        if (children && children instanceof Array) {
          children = rFormat(children);
        }
        console.log("component", component);
        let fmRouter = {
          path: path,
          component: About, // Import about page in the top
          //component: () => import(`./${name}.vue`).then(m => m),
          name: name,
          children: children
        };
        console.log(fmRouter);
        fmRouters.push(fmRouter);
      });
      console.log("fmRouters", fmRouters);
      return fmRouters;
    },
    addRoutes() {}
  }
};
</script>

希望有帮助!