use*_*686 8 subject rxjs angular
有谁为什么这个代码(从主题初始化一个值)不起作用?有没有错误或设计?我究竟做错了什么?
TS
import { Component, OnInit } from '@angular/core';
import { Subject } from "rxjs";
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.styl']
})
export class AppComponent implements OnInit {
itemSupplier$: Subject<any[]> = new Subject<any[]>();
items: any[] = [
{name: 'Item 1', value: 'item1'},
{name: 'Item 2', value: 'item2'},
];
ngOnInit(){
this.itemSupplier$.next(this.items);
}
}
Run Code Online (Sandbox Code Playgroud)
HTML
<ul>
<li *ngFor="let item of itemSupplier$ | async">{{item.name}}</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
这似乎是一个时间问题,如果你把它扔进一个setTimeout就行了.
setTimeout(() => this.itemSupplier$.next(this.items), 0)
编辑
使用BehaviorSubject实际上是一个更好的主意.这将在订阅时提供最后一个值.
归档时间: |
|
查看次数: |
4527 次 |
最近记录: |