為了利用異步管道,我們應該在組件類中創建一個可觀察的選擇器。但是,每個人都建議使用ngOnInit()這樣做:
@Component({
selector: 'app-landing',
templateUrl: './landing.component.html',
styleUrls: ['./landing.component.scss']
})
export class MyComponent implements OnInit {
person$: Observable<Person>;
constructor(private store: Store<ApplicationState>) {}
ngOnInit() {
this.person$ = this.store.select(stateToCurrentPersonSelector);
}
}
如果我們ngOnInit()在下面的代碼片段之外定義可觀察的選擇器會怎樣。它作業得很好:
export class MyComponent implements OnInit {
person$ = this.store.select(stateToCurrentPersonSelector);
constructor(private store: Store<ApplicationState>) {}
ngOnInit() {}
}
uj5u.com熱心網友回復:
在異步管道的情況下,性能方面沒有區別。
您可能認為最好等待 ngOnInit() 觸發產品的獲取,但這是交易。Steam 與 Promise 不同,它是懶惰的。
在訂閱之前,他們不會執行他們的邏輯。
這意味著兩種方法的請求時間沒有區別。
你可以從這里找到更多資訊: https ://twitter.com/realTomaszKula/status/1559258801261404166
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/532579.html
標籤:有角度的ngrx
上一篇:node_modules/@types/lodash/ts4.1/common/lang.d.ts(578,74)中的錯誤:錯誤TS2677:型別謂詞的型別必須可分配給其引數的型別
