Skip to content

Commit 0415226

Browse files
committed
plugins(vuex) and the composition api
1 parent 1939906 commit 0415226

File tree

2 files changed

+36
-26
lines changed

2 files changed

+36
-26
lines changed

src/App.vue

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
11
<template>
2-
<div id="nav">
3-
<router-link to="/">Home</router-link> |
4-
<router-link to="/about">About</router-link>
5-
</div>
6-
<router-view/>
2+
<button @click="increment">Click</button>
3+
<div v-if="count % 2 === 0">Count: {{ count }}. Count is even.</div>
4+
<div v-if="count % 2 !== 0">Count: {{ count }}. Count is odd.</div>
75
</template>
86

7+
<script>
8+
import { computed } from "vue";
9+
import { useStore } from "vuex";
10+
export default {
11+
setup() {
12+
const store = useStore();
13+
const count = computed(() => store.state.count);
14+
const increment = () => {
15+
store.commit("increment");
16+
};
17+
18+
return { count, increment };
19+
},
20+
};
21+
</script>
22+
923
<style>
1024
#app {
1125
font-family: Avenir, Helvetica, Arial, sans-serif;

tests/unit/example.spec.js

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,25 @@
11
import { mount } from "@vue/test-utils";
2-
import { ref } from "vue";
2+
import App from "@/App.vue";
3+
import { createStore } from "vuex";
34

4-
const App = {
5-
setup() {
6-
const count = ref(0);
7-
const increment = () => (count.value += 1);
8-
9-
return { count, increment };
10-
},
11-
template: `
12-
<button @click="increment"></button>
13-
<div v-if="count % 2 === 0">
14-
Count: {{ count }}. Count is even.
15-
</div>
16-
17-
<div v-if="count % 2 !== 0">
18-
Count: {{ count }}. Count is odd.
19-
</div>
20-
`,
5+
const createVuexStore = () => {
6+
return createStore({
7+
state: () => ({
8+
count: 0,
9+
}),
10+
mutations: {
11+
increment(state) {
12+
state.count += 1;
13+
},
14+
},
15+
});
2116
};
2217

23-
function factory({ data } = { data: {} }) {
18+
function factory() {
19+
const store = createVuexStore();
2420
return mount(App, {
25-
data() {
26-
return data;
21+
global: {
22+
plugins: [store],
2723
},
2824
});
2925
}

0 commit comments

Comments
 (0)