一旦用户登录,我就通过权限过滤菜单数组。
我生成静态菜单,然后将数组的副本提供给过滤器。
constructor(public menu: MenuService, public permissionService: PermissionService) {
console.log(menu.getMenu()) // this changes after filtering below
this.menuItems = this.permissionService.filterMenuByTopic([...menu.getMenu()]); // here I'm using a copy
}
为什么会出现这种情况呢?如果我使用了扩展运算符并且不使用原始数组[...menu.getMenu()]。
仅当我刷新页面时才menu.getMenu()返回原始值
UPD 1
回答评论,这里是 getMenu() 函数
import { Injectable } from '@angular/core';
@Injectable()
export class MenuService {
menuItems: Array<any>;
constructor() {
this.menuItems = [];
}
addMenu(items: Array<{
text: string,
heading?: boolean,
link?: string, // internal route links
elink?: string, // used only for external links
target?: string, // anchor target="_blank|_self|_parent|_top|framename"
icon?: string,
alert?: string,
submenu?: Array<any>
}>) {
items.forEach((item) => {
this.menuItems.push(item);
});
}
getMenu() {
return this.menuItems;
}
}
倚天杖
繁花不似锦
相关分类