我有一项名为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();
}
}
它是在app模块中注入的
import {VenueAdminInceptionService} from './services/venueadmin/venueadmin.inception.service';
providers: [VenueService, ModalToggleService, VenueAdminInceptionService],
在我的路由中以下列方式使用服务。
使用VenueadminactualComponent being the observeable with
venueprofileComponent ,
venueadmincateringComponent ,
VenuecreatededitComponent ,
VenueroomadminpageComponent ,
VenueadminsocialmediaComponent`
VenueadminactualComponent
中的observable中的代码:
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);
}
然后每个observable都这样订阅。
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();
}
控制台日志为空白。这让我觉得没有什么可以通过。
我认为它可能是我的应用程序架构,但这就是为什么我提供服务应用程序模块的原因。
路由,如果有意义,虽然我不知道如何,如下:
{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},
]},
请帮忙! Punch和Pie在等待!
答案 0 :(得分: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);
}
);
}
否则它们在subscribe返回数据之前运行(因为订阅是异步发生的)。