|
@@ -61,6 +61,7 @@
|
|
|
import router from "./router";
|
|
|
import { login, logout } from "@/Api/user";
|
|
|
import { mapActions, mapGetters } from "vuex";
|
|
|
+import { getRoutes } from "@/Api/asyncRoute";
|
|
|
export default {
|
|
|
data() {
|
|
|
return {
|
|
@@ -86,16 +87,14 @@ export default {
|
|
|
...mapGetters(["getterPermission"]),
|
|
|
},
|
|
|
created() {
|
|
|
- //加载异步路由
|
|
|
- let asyncRouters = [
|
|
|
- {
|
|
|
- path: "/asyncTest",
|
|
|
- name: "asyncTest",
|
|
|
- alias: "异步菜单",
|
|
|
- component: () => import("@/views/asyncTest/asyncTest.vue"),
|
|
|
- },
|
|
|
- ];
|
|
|
- this.asyncAddRouters(asyncRouters);
|
|
|
+ getRoutes().then((res) => {
|
|
|
+ console.log(res);
|
|
|
+ let ArrRoutes = res.data;
|
|
|
+ console.log("原数据", ArrRoutes);
|
|
|
+ let asyncRouters = this.treeRoutes(ArrRoutes);
|
|
|
+ console.log(asyncRouters);
|
|
|
+ this.asyncAddRouters(asyncRouters);
|
|
|
+ });
|
|
|
},
|
|
|
methods: {
|
|
|
login() {
|
|
@@ -104,12 +103,12 @@ export default {
|
|
|
login(this.user)
|
|
|
.then((res) => {
|
|
|
console.log(res);
|
|
|
- if(res==undefined){
|
|
|
- this.$Cookie.remove()
|
|
|
+ if (res == undefined) {
|
|
|
+ this.$Cookie.remove();
|
|
|
this.$message({
|
|
|
- type: "error",
|
|
|
- message: res,
|
|
|
- });
|
|
|
+ type: "error",
|
|
|
+ message: res,
|
|
|
+ });
|
|
|
}
|
|
|
this.$nextTick(() => {
|
|
|
if (document.cookie !== "") {
|
|
@@ -154,6 +153,36 @@ export default {
|
|
|
}
|
|
|
},
|
|
|
...mapActions(["asyncAddRouters"]),
|
|
|
+ // allRoutes 全部路由对象 ,currentRoute当前点,routers存放点,ifAdd是否插入对象
|
|
|
+ treeRoutes(allRes) {
|
|
|
+ let result = allRes.reduce((prev, item) => {
|
|
|
+ //过滤每一行数据,转成自己想要的,这里重定义变量不要去改变item,为了item.url取值时候可以找的到。
|
|
|
+ let itemFilter = {
|
|
|
+ alias: item.alias,
|
|
|
+ component: () => import(`@/views/${item.url}`),
|
|
|
+ id: item.id,
|
|
|
+ name: item.name,
|
|
|
+ parent: item.parent_id,
|
|
|
+ path: item.path,
|
|
|
+ };
|
|
|
+ //prev 全部数据 item遍历到的一行数据
|
|
|
+ //判断遍历到的这个数据是否有父级,如果有在这父级下面插入子集,否则在转换为键值对象,键为自己的父级id
|
|
|
+ prev[itemFilter.parent]
|
|
|
+ ? prev[itemFilter.parent].push(itemFilter)
|
|
|
+ : (prev[itemFilter.parent] = [itemFilter]);
|
|
|
+ return prev;
|
|
|
+ }, {});
|
|
|
+ for (let prop in result) {
|
|
|
+ //遍历所有键
|
|
|
+ result[prop].forEach((item, i) => {
|
|
|
+ //遍历所有键的值,将所有子集插入到自己键,也就是父级的下面
|
|
|
+ result[item.id] ? (item.children = result[item.id]) : "";
|
|
|
+ });
|
|
|
+ }
|
|
|
+ //选中指定父级的树,null是顶级
|
|
|
+ result = result["null"];
|
|
|
+ return result;
|
|
|
+ },
|
|
|
},
|
|
|
};
|
|
|
</script>
|