I have created a reactive value but I want it to change from the entire application.
To create the reactive state I have src/store/index.js that contains
// ** src/store/index.js
export function useTimer() {
const timer = ref(false);
const toggleTimer = () => {
if (timer.value == false) {
timer.value = true;
} else {
timer.value = false;
}
};
return { timer, toggleTimer };
}
And I am using these values in components
// ** src/app.vue
<template>
<Scores/>
<button @click="switchtimer()">switch at app</button>
</template>
<script>
import { isRef } from "vue";
import { useTimer } from "./store";
import Scores from "./components/Scores.vue";
export default {
components: {
Scores
},
setup() {
const { timer, toggleTimer } = useTimer();
function switchtimer() {
console.log(isRef(timer)); // true
toggleTimer();
}
return { switchtimer };
},
};
</script>
// ** src/components/Scores.vue
<template>
<button @click="switchtimer()">switch at scores</button>
<p>{{ timer }}</p>
</template>
<script>
import { isRef } from "vue";
import { useTimer } from "../store";
export default {
name: "Scores",
setup() {
const { timer, toggleTimer } = useTimer();
function switchtimer() {
console.log(isRef(timer)); // true
toggleTimer();
}
return { timer, switchtimer };
},
};
</script>
Scores.vue is imported in app.vue as component. There is no importing issue in the app even if there is any mistakes I did in this description. That mistake might have occurred during rewriting while posting this.
When switch at scores button pressed the timer toggles in scores component, but switch at app button pressed the timer does not toggles in scores component.
I want to have timer as global state, so that when changed it from app.vue, the changed value also shown in scores.vue.