- Init vue project
- Type this in console
npm i statex-vue
import {createStore} from "statex-vue";
export default createStore({
data() {
return {
test: 0
}
}
});
or
export default createStore({
data: {
test: 0
}
});
import { createApp } from 'vue';
import rootElem from './App.vue';
import store from "@/store";
let app = createApp(rootElem);
app.use(store);
app.mount('#app');
<template>
{{store.global.test}}
</template>
export default createStore({
data: {
test: 0
},
getters: {
getTest() {
return this.global.test.value;
}
}
});
<template>
{{store.global.test}}
{{store.global.getTest()}}
</template>
export default createStore({
data: {
test: 0
},
mutations: {
incrementTest() {
this.global.test.value++;
}
}
});
<template>
{{store.global.test}}
<button @click="store.global.incrementTest()">increment</button>
</template>
export default createStore({
data: {
test: 0
},
methods: {
incrementTest() {
this.global.test.value++;
console.log(this.global.test.value);
return this.global.test.value;
}
}
});
<template>
{{store.global.test}}
<button @click="store.global.incrementTest()">increment</button>
</template>
export default createStore({
data: {
test: 8
},
computed: {
test0() {
return this.global.test.value * this.global.test.value;
}
}
});
<template>
{{store.global.test}}
{{store.global.test0}}
</template>
export default createStore({
data: {
test: 0
},
watch: {
test(newValue) {
console.log("Variable changed", newValue);
}
}
});
<template>
{{store.global.test}}
<button @click="store.global.test++">Change</button>
</template>
import array from "@/store/array";
export default createStore({
modules: {
array
}
});
import { createStoreModule } from "statex-vue";
export default createStoreModule({
data: {
array: ["test", 0, 8, "abcd"]
},
getters: {
getArray() {
return this.array.array.value;
}
},
mutations: {
addElem(elem: any) {
this.array.array.value.push(elem);
}
},
computed: {
double() {
return [...this.array.array.value, ...this.array.array.value];
}
},
methods: {
a() {
this.array.array.value = this.array.double.value;
return this.array.array.value;
}
},
watch: {
array: {
handler(newValue) {
console.log("Variable changed", newValue);
},
deep: true
}
}
});
<template>
{{store.array.array}}
<br>
{{store.array.getArray()}}
<br>
{{store.array.double}}
<br>
<button @click="store.array.addElem(Math.random())">add</button>
<button @click="store.array.a()">a</button>
</template>
export default createStore({
data: {
test: 0
}
}, {
name: "store0"
});
<template>
{{store0.global.test}}
</template>
export default createStore({
data: {
test: 0
}
}, {
defaultModuleName: "default"
});
<template>
{{store.default.test}}
</template>
export default createStore({
data: {
test: 0
}
}, {
defaultModuleName: ""
});
<template>
{{store.test}}
</template>