1

I have a service called VenueAdminInceptionService

import {Subject} from 'rxjs/Subject';
import {Observable} from "rxjs/Observable";
import {VenueAdminInceptionModel} from '../../models/venueadmininceptionmodel/venueadmin.inception.model';

export class VenueAdminInceptionService{

  //pk and url service
  private pkurlsend = new Subject<VenueAdminInceptionModel>();

  sendurlpk(payload: VenueAdminInceptionModel){
    this.pkurlsend.next(payload);
  }

  receiveurlpk(): Observable<VenueAdminInceptionModel>{
    return this.pkurlsend.asObservable();
  }
}

it is injected in the app module

import {VenueAdminInceptionService} from './services/venueadmin/venueadmin.inception.service';
providers: [VenueService, ModalToggleService, VenueAdminInceptionService],

The Service is used in the following way in my routing.

with the VenueadminactualComponent being the observeable with venueprofileComponent,venueadmincateringComponent,VenuecreatededitComponent,VenueroomadminpageComponent,VenueadminsocialmediaComponent`

being the obeservers.

the code in the observable in the VenueadminactualComponent:

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

  ngOnInit() {
    this.inceptedroute = this.route.snapshot.url[0].path;
    if(this.inceptedroute === 'venue-admin'){
      this.permission = 'venue';
    }
    if(this.inceptedroute === 'suits-venue-admin'){
      this.permission = 'suits';
    }

    const payload = {
      pk: this.route.snapshot.params.pk,
      permission: this.permission,
    };
    this.inceptionservice.sendurlpk(payload);

  }

and then each observable is subscribed as such.

constructor(private inceptionservice: VenueAdminInceptionService) { }

  ngOnInit() {
    this.inceptionservicevar = this.inceptionservice.receiveurlpk()
      .subscribe(
        (incep: VenueAdminInceptionModel) => {
          this.permission = incep.permission;
          this.venueid = incep.pk;
        }
      );
    console.log(this.permission);
    console.log(this.venueid);
  }

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

  }

the console logs are blank. Which makes me think nothing is getting through.

I thought it could be my apps architecture, but thats why I provided the service app module wide.

the routing, in case that matters at all, although I can't see how, is as follows:

{path: 'venue-admin/:pk', component: VenueadminactualComponent,
    children:[
      {path: 'profile', component: VenueprofileComponent },
      {path: 'catering', component: VenueadmincateringComponent},
      {path: 'venuepage', component: VenuecreateeditComponent},
      {path: 'room', component: VenueroomadminpageComponent},
      {path: 'socialmedia', component: VenueadminsocialmediaComponent},
    ]},

Assistance please! Punch and Pie await!

4

1 回答 1

2

日志需要在对订阅的响应中:

ngOnInit() {
    this.inceptionservicevar = this.inceptionservice.receiveurlpk()
      .subscribe(
        (incep: VenueAdminInceptionModel) => {
          this.permission = incep.permission;
          this.venueid = incep.pk;
          console.log(this.permission);
          console.log(this.venueid);
        }
      );        
  }

否则它们在订阅返回数据之前运行(因为订阅是异步发生的)。

于 2018-01-29T17:43:12.227 回答