服务中的主题,但订阅时没有数据通过。 [角4]

时间:2018-01-29 22:45:18

标签: angular angular2-observables subject

我在app模块上提供了一项服务:

服务:

providers: [VenueService, ModalToggleService, VenueAdminInceptionService],

在应用模块上提供:

 constructor(private inceptionservice: VenueAdminInceptionService, private route: ActivatedRoute, private router: Router) { }

  navipush(pk){
    const payload = {
      pk: pk,
      permission: this.route.snapshot.params.permission,
    };
    this.inceptionservice.sendurlpk(payload);
    this.router.navigate(['/admin', 'venue-admin', payload.permission, payload.pk,'profile']);
  }

服务在一个组件中被触发:

export class TabandtitleviewComponent implements OnInit, OnDestroy {
  inceptionservicevar;
  permission;
  venueid;
  venueadmin = false;
  suitsadmin = false;

  constructor(private inceptionservice: VenueAdminInceptionService) { }

  ngOnInit() {
    this.inceptionservicevar = this.inceptionservice.receiveurlpk()
      .subscribe(
        (incep: VenueAdminInceptionModel) => {
          this.permission = incep.permission;
          this.venueid = incep.pk;
          console.log('it happend');
          if(this.permission === 'venue'){
            this.venueadmin = true;
            this.suitsadmin = false;
          }
          if(this.permission === 'suits'){
            this.venueadmin = false;
            this.suitsadmin = true;
          }
        }
      );
  }

  ngOnDestroy(){
    this.inceptionservicevar.unsubscribe();

  }

并订阅另一个:

<div id="navtabrow" class="row"> <!-- nav tab row-->
  <!-- venue admin view-->
  <a *ngIf=" venueadmin" [routerLink]="['/admin','venue-admin',venueid, 'profile']">
    <div class="navtab">
      Venue Profile
    </div>
  </a>
  <!-- suits admin view-->
  <a *ngIf="suitsadmin" [routerLink]="['/admin','suits-venue-admin',venueid,'profile']">
    <div class="navtab">
      Venue Profile
    </div>
  </a>
订阅组件逻辑的html模板中的

用于显示一个或另一个按钮:

{{1}}

因为两个按钮都不会显示在组件init上。他们仍然处于虚假状态。这意味着观察者没有看到变化。

发生了什么事?你需要看看我的建筑吗?我认为这无关紧要,因为该服务适用于各种应用程序。

1 个答案:

答案 0 :(得分:0)

这就是答案:

使用Behavior Subject代替Subject;

BehaviorSubject vs Observable?