Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/415.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在angular 7中使用ng内容动态隐藏子对象_Javascript_Angular_Components_Parent Child_Angular7 - Fatal编程技术网

Javascript 在angular 7中使用ng内容动态隐藏子对象

Javascript 在angular 7中使用ng内容动态隐藏子对象,javascript,angular,components,parent-child,angular7,Javascript,Angular,Components,Parent Child,Angular7,在我的项目中有一个场景,其中内容必须根据为特定登录用户提供的角色权限隐藏 因此,我们制作了一个名为的全局组件,它将根据用户拥有的权限启用子组件 组件。ts import { Component, Input, ChangeDetectionStrategy } from '@angular/core'; import { GlobalService } from '../../../core/global/global.service'; @Component({ selector: 'a

在我的项目中有一个场景,其中内容必须根据为特定登录用户提供的角色权限隐藏

因此,我们制作了一个名为
的全局组件,它将根据用户拥有的权限启用子组件

组件。ts

import { Component, Input, ChangeDetectionStrategy } from '@angular/core';
import { GlobalService } from '../../../core/global/global.service';

@Component({
  selector: 'app-authorise',
  templateUrl: './app-authorise.component.html',
  styleUrls: ['./app-authorise.component.scss'],
  changeDetection: ChangeDetectionStrategy.Default
})
export class AuthoriseComponent {
  @Input() public module: string;
  @Input() public permission: string;
  @Input() public field: string;
  @Input() public role: string;

  public currentUser: any = {};
  public currentUserRoles = [];
  constructor(private globalService: GlobalService) {
    this.globalService.subscribeToUserSource((updatedUser: any) => {
      this.currentUser = updatedUser;
      this.currentUserRoles = updatedUser.rolePermissions;
    });
  }

  get enable() {
    const {
      currentUser,
      currentUserRoles,
      module,
      permission,
      role
    } = this;
    if (currentUser && currentUserRoles) {
      return role ? this.hasRole(currentUserRoles, role) :
      this.globalService.hasPermissionForModule({
        currentUserRoles,
        module,
        permission,
      });
    }
    return false;
  }

  public hasRole(currentUserRoles: any, role: string) {
    return Boolean(currentUserRoles[role]);
  }
}
Component.html

<ng-container>
  <ng-content *ngIf="enable"></ng-content>
</ng-container>

用例

<app-authorise [module]="properties.modules.project" [permission]="properties.permissions.CREATE">
  <app-psm-list></app-psm-list>
</app-authorise>

我们面临的实际问题是,即使在父组件内部启用了子组件,也会调用子组件的onInit()方法


任何想法,这方面的建议都会非常有用。

您可以在投影之前检查条件。

发现这非常有用。
可以使用指令而不是组件。

尝试创建动态组件,而不是投影组件,其效果就像一个符咒。谢谢你,阿米特。现在必须更改代码:)
<app-authorise #authorise [module]="properties.modules.project" [permission]="properties.permissions.CREATE">
  <ng-conatiner *ngIf="authorise.enable">
      <app-psm-list></app-psm-list>
  </ng-conatiner>
</app-authorise>
<ng-container>
  <ng-content></ng-content>
</ng-container>