Vue/Vuex beginner here. Is there a way to set the initial state in Vuex dynamically? I have a boolean state called is_here (true if a member is present), whose value I want to check dynamically with a conditional statement before setting.
If I try to compile the code like below, it returns this error: TS2564: Property 'is_here' has no initializer and is not definitely assigned in the constructor.
import { Action, Module, Mutation, VuexModule, getModule } from 'vuex-module-decorators';
export interface IMemberState {
is_here: boolean;
}
@Module({dynamic: true, store, name: 'member', namespaced: true})
class Member extends VuexModule implements IMemberState {
public is_here: boolean // The app expects me to set true or false here
}
If I set the default value for the initializer to true or false, the app compiles correctly. But if I change the status (let's say from true to false) and refresh the page, the state reverts back to true (a different button is rendered based on this boolean, so I can see it's reverted back to true).
public is_here: boolean = true
What I want to do is to make an API call and check certain things before setting the is_here state. I wrote an @Action that does the necessary checks.
@Action({})
public async setHereStatus() {
await axios.get('api/member_status').then((response)=>
if(response.here_status) {
// This action returns true or false
)
}
I tried putting this @Action instead of hardcoding the value for is_here would work, but I got this error: TS2322: Type '() => Promise<boolean>' is not assignable to type 'boolean'.
public is_here: boolean = this.setHereStatus()
How can I assign this state dynamically in this kind of scenario? Am I supposed to use something like created or mounted?
[Update] As @ittus commented, I should've used a @Mutation to set the is_here status. I've been experimenting like this.
@Module({dynamic: true, store, name: 'member', namespaced: true})
class Member extends VuexModule implements IMemberState {
public is_here: boolean = false
@Mutation
public SET_HERE_STATUS(status: boolean): void {
this.is_here = status
}
@Action({})
public async setHereStatus() {
await axios.get('api/member_status').then((response)=>
if(response.here_status) {
this.SET_HERE_STATUS(true)
}else {
this.SET_HERE_STATUS(false)
}
}
// In the Vue component where I use this state
created () {
memberModule.setHereStatus()
}
However, the same issues persist; if I refresh the page, or close the window and access the same URL again, the state is reset. I don't know if my created hook is functioning properly.