State not reactive on other file #1739
-
Maybe I'm using it wrong, but I thought something like this should work:
// app.vue
//home.vue
Am I misunderstanding something here? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
that is absolutely expectable, to mutate the state you should have an actions within pinia state, not just modifying message directly, (on top of this, onMounted is not reliable since is passing once the component is mounted, and after this you lose the changes) try something like: {
state: () => ({
rawExample: ''
}),
getters: {
example: (state) => {
return state.rawExample;
}
},
actions: {
setMessage(message) {
this.example = message;
},
},
} app.vue <script setup>
import { mainStore } from "../store/store";
const store = mainStore();
store.setMessage('wang wing wung')
</script> and now all should be reactive, for good practices you want to avoid accessing and mutating the state directly, use getters (to access) and actions (to modify/mutate). |
Beta Was this translation helpful? Give feedback.
-
yes, this should work (you don't need an action). You should try putting a demo here, there must be something missing somewhere, e.g. you are only showing the |
Beta Was this translation helpful? Give feedback.
-
[SOLVED]
then I just turn it over |
Beta Was this translation helpful? Give feedback.
[SOLVED]
solved guys,
this is because i initialize router above pinia,
then I just turn it over