简体   繁体   English

如何在可观察数组中注入 promise 数组

[英]How to inject promise array inside observable array

I'm building a user menu based on authentication and everything work, but I have part of that menu I get it from an API getMainCategories() a promise type method.我正在构建一个基于身份验证的用户菜单并且一切正常,但我有一部分菜单是从 API getMainCategories()一个promise类型的方法中获得的。

I need to inject the coming data from that method inside my menu array on a specific object;我需要将来自该方法的数据注入到特定 object 的菜单数组中;

Here:这里:

let menu: any = [
  ...
  {
    text: 'categories',
    icon: 'flaticon-2-squares',
    children: [] // I need to inject the categories here
  },
  ...
];

On the code below you will notice that I have converted the promise getMainCategories into observable but I can't figure out what next.在下面的代码中,您会注意到我已将 promise getMainCategories转换为observable ,但我不知道接下来要做什么。

My Menu code:我的菜单代码:

getMenu() {

  const mainCategories = from(this.woo.getMainCategories());

  let menu: any = [
    {
      text: 'home',
      icon: 'flaticon-home',
      path: '/tabs/home'
    },
    {
      text: 'categories',
      icon: 'flaticon-2-squares',
      children: []
    },
    {
      text: 'cart',
      icon: 'flaticon-shopping-cart',
      path: '/tabs/cart'
    },
    {
      text: 'chechout',
      icon: 'flaticon-shopping-bag',
      path: '/checkout'
    }
  ];

  const loggedInMenu = [
    {
      text: 'My Account',
      icon: 'flaticon-login',
      path: '/tabs/profile'
    }
  ];

  const loggedOutMenu = [
    {
      text: 'login',
      icon: 'flaticon-login',
      path: '/login'
    },
    {
      text: 'register',
      icon: 'flaticon-login',
      path: '/register'
    }
  ];

  const menu$ = this.isUserLoggedIn.pipe(
    switchMap(
      status => {
        if (status) {
          return of(menu.concat(loggedInMenu))
        } else {
          return of(menu.concat(loggedOutMenu))
        }
      }
    )
  );

  return menu$;

}

I don't know what "of" is.我不知道“的”是什么。 But I think you may want to let menu.children.concat(loggedXXXMenu) rather than menu.concat.但我认为您可能希望让 menu.children.concat(loggedXXXMenu) 而不是 menu.concat。

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

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