我正在使用Angular v2.1
,我正在尝试让组件通过共享服务发送消息。
我想首先将数据从第一个组件company.component.ts
发送到第二个组件modal.component.ts
。
我不知道问题出在哪里(如果是在发送或接收数据时)。当我调试接收数据时,我发现了一个订阅者对象:
Subscriber {closed: false, syncErrorValue: null, syncErrorThrown: false, syncErrorThrowable: false, isStopped: false…}
在modal.service.ts
:
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs/Subject';
@Injectable()
export class ModalService {
private sharedCompanySource = new Subject<string>();
sharedCompany = this.sharedCompanySource.asObservable();
getCompany(){
return this.sharedCompany;
}
setCompany(_company: any){
this.sharedCompanySource.next(_company);
}
}
在第一个组件中company.component.ts
:
import { ModalService } from './modals/modal.service';
@Component({
templateUrl: 'company.component.html',
providers: [
ModalService
]
})
export class CompanyComponent implements OnInit {
constructor(private missionService: ModalService) {
this.missionService.setCompany("hellooooooooooooooo");
}
}
在第二个组件中modal.component.ts
:
import { ModalService } from './modals/modal.service';
import { Subscription } from 'rxjs/Subscription';
@Component({
templateUrl: 'modal.component.html',
providers: [
ModalService
]
})
export class ModalComponent implements OnInit {
subscription:Subscription;
constructor(private missionService: ModalService) {
this.subscription = missionService.sharedCompany.subscribe();
}
}