The code below is based on vue3 with typescript used.
export interface TenantDto {
uuid: string;
name: string;
}
export const useTenantStore = defineStore('tenant', {
state: () => ({
tenants: [],
}),
actions: {
setMyTenants: (payload: TenantDto[]) => {
this.tenants = payload;
},
}
);
compiling the code in production produces a error on the line
this.tenants = payload;
The error is saying Object is possibly ‘undefined’.
how would I get it around ?
>Solution :
You cannot use arrow function to define actions since it relies on the this keyword.
Try this instead:
export const useTenantStore = defineStore('tenant', {
state: () => ({
tenants: [],
}),
actions: {
setMyTenants(payload: TenantDto[]) {
this.tenants = payload;
}
}
});