尽管在模块级别 angular 4 提供了注入服务,但仍未正确执行
injected service not executing correctly despite being provided at module level angular 4
我有一个名为 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],
Service在我的路由中是这样使用的。
与 VenueadminactualComponent being the observeable with
venueprofileComponent,
venueadmincateringComponent,
VenuecreatededitComponent,
VenueroomadminpageComponent,
VenueadminsocialmediaComponent`
作为观察者。
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},
]},
求助!潘趣酒和馅饼等着你!
日志需要在对订阅的响应中:
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);
}
);
}
否则它们在订阅返回数据之前 运行(因为订阅是异步发生的)。
我有一个名为 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],
Service在我的路由中是这样使用的。
与 VenueadminactualComponent being the observeable with
venueprofileComponent,
venueadmincateringComponent,
VenuecreatededitComponent,
VenueroomadminpageComponent,
VenueadminsocialmediaComponent`
作为观察者。
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},
]},
求助!潘趣酒和馅饼等着你!
日志需要在对订阅的响应中:
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);
}
);
}
否则它们在订阅返回数据之前 运行(因为订阅是异步发生的)。