У меня такая проблема, я делаю демонстрационную веб-страницу, которая опирается на систему блогов в angular8. Проблема возникает там:
, когда я использую веб-ссылку для маршрутизации к указанному посту, развернутому, тогда это работает. Но когда я использую напрямую / post-details / 3, это не так.
posts.component.ts
postDetailsClick(i){
this.getSharedBodies.subject.next(this.body); //setting BehaviorSubject
this.getSharedTitles.subject.next(this.titles);
this.router.navigate(['/post-details', i]);
}
posts.component.html:
<mat-grid-list cols="3" rowHeight="150px">
<mat-grid-tile
*ngFor="let post of showPosts; let i = index"
[colspan]="post.cols"
[rowspan]="post.rows"
[style.background]="post.color">
{{post.text}}
<p><button (click)="postDetailsClick(i)" >Try it</button></p>
<br>
</mat-grid-tile>
</mat-grid-list>
post-details.component.html
export class PostDetailsComponent implements OnInit {
indexOfPost;
titles;
// bodies = [];
subscription: Subscription;
renderPost:boolean=false;
constructor(private activatedRoute:ActivatedRoute,private getSharedTitles:ShareInfoTitleService,private getSharedBodies: ShareInfoBodyService) {}
ngOnInit() {
this.indexOfPost=this.activatedRoute.snapshot.paramMap.get("id");
console.log(this.getSharedTitles.subject.asObservable());
}
}
line: console.log (this.getSharedTitles.subject.asObservable ());возвращает нулевой наблюдаемый при прямом доступе по URL. Но при нажатии на кнопку все работает нормально.
export class ShareInfoTitleService {
subject = new BehaviorSubject<any>(null);
constructor() { }
sendMessage(message: any[]) {
this.subject.next({ titles: message });
}
clearMessages() {
this.subject.next(null);
}
getMessage(): Observable<any> {
return this.subject.asObservable();
}
}