Let's say I have a model User.
I want to create a BehaviorSubject of type User as below:
private userSource = new BehaviorSubject<User>({});
Run Code Online (Sandbox Code Playgroud)
With this statement I'm getting the following error:
Argument of type '{}' is not assignable to parameter of type 'User'
Run Code Online (Sandbox Code Playgroud)
Could anyone help me how define a BehaviorSubject of type User
new BehaviorSubject<User>({}) with this you have passed an initial value for the BehaviorSubject. Cause {} has no type detection, you get error.
You need to pass an User type. Provide properties for that type in the {} like
private userSource = new BehaviorSubject<User>({ /* properties */ });
Run Code Online (Sandbox Code Playgroud)
If you don't need initial value. you can use ReplaySubject with replay count set to 1
new ReplaySubject<User>(1)
Run Code Online (Sandbox Code Playgroud)
This will provide same functionality without initial value.
那是因为{}是一个对象与User(实际上它是一个空对象)没有相同的属性。
您可以通过强制转换{}为any:
new BehaviorSubject<User>({} as any);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
638 次 |
| 最近记录: |