8由A组件,跳到B组件.
一. A组件点击事件:
import { ListlevelService } from 'src/app/services/listlevel.service';
... ...
toInfo(id: string) {
const projectChild = this.projectsTop.filter(p => p.parentProjectId = id);
if(projectChild.length > 0) {
console.log(projectChild) //数组有值
this.listlevelServe.sendMessage(projectChild) //传值
this.router.navigate(['/sheetInfo', id]);
}
}
二. service 组件:
@Injectable({
providedIn: ServicesModule
})
export class ListlevelService {
private subject = new Subject<any>();
sendMessage(type:any){
console.log('TAG'+'--->'+type) //能打印
this.subject.next({type});
}
getMessage():Observable<any> {
return this.subject.asObservable();
}
}
三. B组件:
import { ListlevelService } from 'src/app/services/listlevel.service';
... ...
// 接收消息的地方:
ngAfterViewInit():void {
console.log('1111') //能打印
this.subscription = this.listlevelServe.getMessage().subscribe(
projectChild => {
console.log(projectChild)
this.projectChild = projectChild
console.log('3333') //不能打印
})
console.log('2222') //能打印
console.log(this.subscription)
}
问题出在哪里 ?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…