admin管理员组文章数量:1026925
There might be someone who knows why NGXS state cannot change inside of HttpClient POST request.
ctx.patchState()
Only works outside HTTP POST request.
@Action(SignIn)
signin(ctx: StateContext<AppStateModel>, { payload }: SignIn) {
// ctx.patchState({isLoggedIn:true}) this works!
return this.api$.signin(payload)
.pipe(
tap((user: User) => {
console.log(user);
ctx.patchState({ isLoggedIn: true })
})
)
.subscribe(
(user: User) => {
ctx.patchState({ isLoggedIn: true })
}
)
}
There might be someone who knows why NGXS state cannot change inside of HttpClient POST request.
ctx.patchState()
Only works outside HTTP POST request.
@Action(SignIn)
signin(ctx: StateContext<AppStateModel>, { payload }: SignIn) {
// ctx.patchState({isLoggedIn:true}) this works!
return this.api$.signin(payload)
.pipe(
tap((user: User) => {
console.log(user);
ctx.patchState({ isLoggedIn: true })
})
)
.subscribe(
(user: User) => {
ctx.patchState({ isLoggedIn: true })
}
)
}
Share
Improve this question
edited Aug 26, 2018 at 19:28
R. Richards
25.2k10 gold badges66 silver badges65 bronze badges
asked Aug 26, 2018 at 19:20
Nacho CastilloNacho Castillo
532 silver badges6 bronze badges
4
-
Is your
console.log
statement from inside thetap
being printed? – user184994 Commented Aug 26, 2018 at 19:32 - Yes, the data is printed, but state not change, only change when I call ctx.patchState() outside of subscribe. – Nacho Castillo Commented Aug 26, 2018 at 19:36
- You should really emit an action with payload that data were loaded. Also don't subscribe inside of the action. Ngxs will subscribe for you. – Martin Nuc Commented Aug 26, 2018 at 19:40
- Thank you!! that's work better, I do not know that ngxs subscribe the observables , thank you. – Nacho Castillo Commented Aug 26, 2018 at 19:46
1 Answer
Reset to default 6Actually, the state is changing, but you don't see it because you return subscription that hasn't been pleted. In other words - You'll see the action being dispatched once the subscription of the returned observable pletes.
As mentioned in the ments, the returned observable of the actions are being subscribed behind the scene, so there's no need to subscribe to it again.
After that being said, you can pass take(1)
in the pipe.
What it does, it pletes the subscription of the observable after it triggered once.
@Action(SignIn)
signin(ctx: StateContext<AppStateModel>, { payload }: SignIn) {
return this.api$.signin(payload)
.pipe(
take(1), // <-- Add that
tap((user: User) => ctx.patchState({ isLoggedIn: true }))
);
}
There might be someone who knows why NGXS state cannot change inside of HttpClient POST request.
ctx.patchState()
Only works outside HTTP POST request.
@Action(SignIn)
signin(ctx: StateContext<AppStateModel>, { payload }: SignIn) {
// ctx.patchState({isLoggedIn:true}) this works!
return this.api$.signin(payload)
.pipe(
tap((user: User) => {
console.log(user);
ctx.patchState({ isLoggedIn: true })
})
)
.subscribe(
(user: User) => {
ctx.patchState({ isLoggedIn: true })
}
)
}
There might be someone who knows why NGXS state cannot change inside of HttpClient POST request.
ctx.patchState()
Only works outside HTTP POST request.
@Action(SignIn)
signin(ctx: StateContext<AppStateModel>, { payload }: SignIn) {
// ctx.patchState({isLoggedIn:true}) this works!
return this.api$.signin(payload)
.pipe(
tap((user: User) => {
console.log(user);
ctx.patchState({ isLoggedIn: true })
})
)
.subscribe(
(user: User) => {
ctx.patchState({ isLoggedIn: true })
}
)
}
Share
Improve this question
edited Aug 26, 2018 at 19:28
R. Richards
25.2k10 gold badges66 silver badges65 bronze badges
asked Aug 26, 2018 at 19:20
Nacho CastilloNacho Castillo
532 silver badges6 bronze badges
4
-
Is your
console.log
statement from inside thetap
being printed? – user184994 Commented Aug 26, 2018 at 19:32 - Yes, the data is printed, but state not change, only change when I call ctx.patchState() outside of subscribe. – Nacho Castillo Commented Aug 26, 2018 at 19:36
- You should really emit an action with payload that data were loaded. Also don't subscribe inside of the action. Ngxs will subscribe for you. – Martin Nuc Commented Aug 26, 2018 at 19:40
- Thank you!! that's work better, I do not know that ngxs subscribe the observables , thank you. – Nacho Castillo Commented Aug 26, 2018 at 19:46
1 Answer
Reset to default 6Actually, the state is changing, but you don't see it because you return subscription that hasn't been pleted. In other words - You'll see the action being dispatched once the subscription of the returned observable pletes.
As mentioned in the ments, the returned observable of the actions are being subscribed behind the scene, so there's no need to subscribe to it again.
After that being said, you can pass take(1)
in the pipe.
What it does, it pletes the subscription of the observable after it triggered once.
@Action(SignIn)
signin(ctx: StateContext<AppStateModel>, { payload }: SignIn) {
return this.api$.signin(payload)
.pipe(
take(1), // <-- Add that
tap((user: User) => ctx.patchState({ isLoggedIn: true }))
);
}
本文标签: javascriptNGXS State not changingStack Overflow
版权声明:本文标题:javascript - NGXS State not changing - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745651094a2161323.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论