File size: 2,583 Bytes
4d70170 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
strict: true,
state: {
inited: 0,
count: 0,
lastCountPayload: null,
date: new Date(),
set: new Set(),
map: new Map(),
sym: Symbol('test'),
object: {
name: 'I am Object',
number: 0,
children: [
{
number: 0,
},
],
},
},
mutations: {
TEST_INIT: state => state.inited++,
INCREMENT: (state, payload) => {
state.count++
state.lastCountPayload = payload
},
DECREMENT: (state, payload) => {
state.count--
state.lastCountPayload = payload
},
UPDATE_DATE: (state) => {
state.date = new Date()
},
TEST_COMPONENT: (state) => { /* noop */ },
TEST_SET: (state) => {
state.set.add(Math.random())
},
TEST_MAP: (state) => {
state.map.set(`mykey_${state.map.size}`, state.map.size)
},
},
actions: {
ASYNC_INCREMENT: ({ commit }) => {
return wait(100).then(() => {
commit('INCREMENT', 1)
})
},
},
getters: {
isPositive: state => state.count >= 0,
hours: state => state.date.getHours(),
errorGetter: () => {
throw new Error('Error from getter')
},
},
modules: {
'nested': {
namespaced: true,
state() {
return {
foo: 'bar',
}
},
getters: {
twoFoos: state => state.foo.repeat(2),
dummy: () => {
console.log('dummy getter was computed')
return 'dummy'
},
},
mutations: {
ADD_BAR: (state) => {
state.foo += 'bar'
},
REMOVE_BAR: (state) => {
state.foo = state.foo.substring('bar'.length)
},
},
modules: {
nestedNested: {
state() {
return {
answer: 42,
}
},
getters: {
doubleAnswer: state => state.answer * 2,
errorGetter: () => {
throw new Error('Error from getter')
},
},
},
},
},
'notNamespaced': {
state() {
return {
hello: 'world',
}
},
getters: {
hello2: state => state.hello.repeat(2),
},
},
'use/in/name': {
state() {
return {
meow: 'MEOW',
}
},
getters: {
meow2: state => state.meow.repeat(2),
},
},
},
})
function wait(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms)
})
}
|