我有一个叫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();
}
}它被注入应用程序模块中。
import {VenueAdminInceptionService} from './services/venueadmin/venueadmin.inception.service';
providers: [VenueService, ModalToggleService, VenueAdminInceptionService],在我的路由中,该服务的使用方式如下。
与venueprofileComponent,venueadmincateringComponent,VenuecreatededitComponent,VenueroomadminpageComponent,VenueadminsocialmediaComponent`的VenueadminactualComponent being the observeable with
做调查员。
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);
}然后每一个可观测到的都是这样的。
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},
]},请帮忙!潘趣和派等着你!
发布于 2018-01-29 17:43:12
日志需要在对订阅的响应中:
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);
}
);
}否则,它们将在订阅返回数据之前运行(因为订阅是异步发生的)。
https://stackoverflow.com/questions/48506973
复制相似问题