简体   繁体   English

无限分配TypeScript内存?

[英]Unlimited allocation of memory TypeScript?

I have custom property that stores objects of class Menu: 我具有存储菜单类的对象的自定义属性:

private menu: Menu[] = [];

Where class Menu : class Menu

export class Menu {
  constructor(public url: string, public title: string, public submenu: Menu[], public role: string, public avaliable: boolean) {
  }
}

In class service MenuService I fill up this.menu : 在班级服务MenuService我填写了this.menu

 this.menu = [
      new Menu('/dashboard/p', 'menu_zavuc_1', null, 'Principal', true),
      new Menu('/school/%school_id%/h/management', 'menu_zavuc_2', null, 'Principal', true),
      new Menu('/school/%school_id%/h/planning', 'menu_zavuc_3', null, 'Principal', true)];

There is another method in the same class MenuService : 同一类MenuService还有另一个方法:

public getMenuItems(profile: MenuHeader): IMenuItem[] {

    let arr: IMenuItem[] = [];
    this.menu.filter(function (element: Menu) {
      return element.role == profile.role();
    });

    let arrOut: IMenuItem[] = [];

    for (let i = 0; i < arr.length; i++) {

       let url = arr[i].url.replace('%school_id%', profile.organizationId().toString());
      url = url.replace('%pupil_id%', profile.getId().toString());

      arrOut.push({
        url: arr[i].url,
        title: arr[i].title,
        role: arr[i].role
      });
    }
    return arrOut;
  } 

Method getMenuItems is called from template: 从模板中调用方法getMenuItems

<div *ngFor="let menu of menuService.getMenuHeaders()">
   <div *ngFor="let item of menuService.getMenuItems(menu)"></div>
</div>

As you can see it is loop in loop. 如您所见,它是循环循环的。

Problem is when I call method getMenuItems() it invokes unlimited memory allocation. 问题是当我调用方法getMenuItems()它将调用无限的内存分配。

I think cause is in this.menu.filter() when I try reuse the same object this.menu for some operations. 我认为当我尝试将同一对象this.menu用于某些操作时,原因在this.menu.filter() Possible each time this.menu is linked to previous values. 每次this.menu链接到以前的值时this.menu可能出现。

Maybe to clone this.menu ? 也许要克隆this.menu

If comment body of getmenuItems() and write only: console.log(profile.role()); 如果注释getmenuItems()正文,并且仅写: console.log(profile.role()); it works fine 它工作正常

If rewrite function on: 如果重写功能在:

public getMenuItems(profile: MenuHeader): IMenuItem[] {
    let list: IMenuItem[] = [];


    let l = {
      url: '',
      title: 'sss',
      role: 'sdadad'
    };

    let a = {
      url: '',
      title: 'sss',
      role: 'sdadad'
    };

    list.push(l);
    list.push(a);

    return list;
}

It works fine! 工作正常!

I am not sure if this will fix your memory problem, but it seems to me that there are a few mistakes in the code. 我不确定这是否可以解决您的内存问题,但是在我看来,代码中存在一些错误。

Be aware that .filter on an array does not change the array, it only RETURNS a filtered array. 请注意, .filter上的.filter不会更改该数组,它只会返回已过滤的数组。

this.menu.filter(function (element: Menu) {
      return element.role == profile.role();
    });

Also you are looping over arr but that variable stays initialized with []. 另外,您正在遍历arr但是该变量保持使用[]初始化。

So, did you mean the following? 那么,您是说以下意思吗?

let arr: IMenuItem[] = this.menu.filter(function (element: Menu) {
      return element.role == profile.role();
    });

Also do you really want to return arr ? 您还真的要返回arr吗? It seems that you really wanted to return arrOut 看来您确实想返回arrOut

That could be written as: 可以这样写:

public getMenuItems(profile: MenuHeader): IMenuItem[] {
    return this.menu
      .filter( (element: Menu) => element.role == profile.role() )
      .map( (element: Menu):MenuItem => {
          return { 
              url: element.url,
              title: element.title,
              role: element.role
          }
      });
}

In this way, all your operations on "this.menu" are pure local in your method. 这样,您对“ this.menu”的所有操作在您的方法中都是纯本地的。

Perhaps that helps to tackle your memory problem 也许这有助于解决您的记忆力问题

warm regards 温暖的问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM