内容来自尚硅谷教学课件
const state = {
xxx: initValue
}
const mutations = {
yyy (state, {data1}) {
// 更新state 的某个属性
}
}
const actions = {
zzz ({commit, state}, data1) {
commit('yyy', {data1})
}
}
$store.getters.xxx
const getters = {
mmm (state) {
return ...
}
}
export default new Vuex.Store({
state,
mutations,
actions,
getters
})
import {mapState, mapGetters, mapActions} from 'vuex'
export default {
computed: {
...mapState(['xxx']),
...mapGetters(['mmm']),
}
methods: mapActions(['zzz'])
}
{{xxx}} {{mmm}} @click="zzz(data)"
import store from './store'
new Vue({
store
})
/**
* vuex 的store 对象模块
*/
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
/*
state 对象
类似于data
*/
const state = {
count: 0 // 初始化状态数据
}
/*
mutations 对象
包含个方法: 能直接更新state
一个方法就是一个mutation
mutation 只能包含更新state 的同步代码, 也不会有逻辑
mutation 由action 触发调用: commit('mutationName')
*/
const mutations = {
//增加的mutation
INCREMENT(state) {
state.count++
},
//减少的mutation
DECREMENT(state) { // ctrl + shift + x
state.count--
}
}
/*
actions 对象
包含个方法: 触发mutation 调用, 间接更新state
一个方法就是一个action
action 中可以有逻辑代码和异步代码
action 由组件来触发调用: this.$store.dispatch('actionName')
*/
const actions = {
//增加的action
increment({
commit
}) {
//提交增加的mutation
commit('INCREMENT')
},
//减少的action
decrement({
commit
}) {
//提交减少的mutation
commit('DECREMENT')
},
//带条件的action
incrementIfOdd({
commit,
state
}) {
if (state.count % 2 === 1) {
commit('INCREMENT')
}
},
//异步的action
incrementAsync({
commit
}) {
//在action中直接就可以执行异步代码
setTimeout(() => {
commit('INCREMENT')
}, 1000)
}
}
/*
getters 对象
包含多个get 计算计算属性方法
*/
const getters = {
oddOrEven(state) { //不需要调用,只需要读取属性值
return state.count % 2 === 0 ? '偶数' : '奇数'
},
count(state) {
return state.count
}
}
// 向外暴露store 实例对象
export default new Vuex.Store({
//构造函数
state, //状态
mutations, //包含多个更新state函数的对象
actions, //包含多个对应事件回调函数的对象
getters //包含多个getter计算属性函数的对象
})
import Vue from 'vue'
import App from './App.vue'
import store from './store'
new Vue({
el: '#app',
components: {App},
template: ' ',
store // 所有组件都多个一个属性: $store
})
clicked: {{ $store.state.count }} times, count is {{ oddOrEven }}
clicked: {{ count }} times, count is {{ oddOrEven2 }}
/**
* 包含多个mutation name
*/
export const RECEIVE_TODOS = 'receive_todos'
export const ADD_TODO = 'add_todo' //添加todo
export const REMOVE_TODO = 'remove_todo'
export const DELETE_DONE = 'delete_done'
export const UPDATE_ALL_TODOS = 'update_all_todos'
//包含多个由action触发去直接更新状态的方法对象
import {
RECEIVE_TODOS,
ADD_TODO,
REMOVE_TODO,
DELETE_DONE,
UPDATE_ALL_TODOS
} from './types'
export default {
[RECEIVE_TODOS](state, {
todos
}) {
state.todos = todos
},
[ADD_TODO](state, {
todo
}) {
state.todos.unshift(todo)
},
[REMOVE_TODO](state, {
index
}) {
state.todos.splice(index, 1)
},
[DELETE_DONE](state) {
state.todos = state.todos.filter(todo => !todo.complete)
},
[UPDATE_ALL_TODOS](state, {
isCheck
}) {
state.todos.forEach(todo => todo.complete = isCheck)
}
}
// 包含多个接受组件通知触发mutation调用,间接更新状态的方法对象
import storageUtil from '../util/storageUtil'
import {
RECEIVE_TODOS,
ADD_TODO,
REMOVE_TODO,
DELETE_DONE,
UPDATE_ALL_TODOS
} from './types'
export default {
readTodo({
commit
}) {
setTimeout(() => {
const todos = storageUtil.fetch()
// 提交commit 触发mutation 调用
commit(RECEIVE_TODOS, {
todos
})
}, 1000)
},
addTodo({
commit
}, todo) {
commit(ADD_TODO, {
todo
})
},
removeTodo({
commit
}, index) {
commit(REMOVE_TODO, {
index
})
},
deleteDone({
commit
}) {
commit(DELETE_DONE)
},
updateAllTodos({
commit
}, isCheck) {
commit(UPDATE_ALL_TODOS, {
isCheck
})
}
}
// 含有多个计算属性对象
export default {
todos(state) {
return state.todos
},
totalSize(state) {
return state.todos.length
},
completeSize(state) {
return state.todos.reduce((preTotal, todo) => {
return preTotal + (todo.complete ? 1 : 0)
}, 0)
},
isAllComplete(state, getters) {
return getters.totalSize === getters.completeSize && getters.totalSize > 0
}
}
/*
vuex最核心的管理对象store
*/
import Vue from 'vue'
import Vuex from 'vuex'
import mutations from './mutations'
import actions from './actions'
import getters from './getters'
Vue.use(Vuex)
const state = {
todos: []
}
export default new Vuex.Store({
state,
mutations,
actions,
getters
})
// 使用localStorage存储数据的工具模块
// 1.函数 :1个功能
// 2.对象 :2个功能
// 需要向外暴露1个功能还是多个功能
var STORAGE_KEY = 'todos'
export default {
fetch() {
return JSON.parse(localStorage.getItem(STORAGE_KEY) || '[]')
},
save(todos) {
localStorage.setItem(STORAGE_KEY, JSON.stringify(todos))
}
}
/*base*/
body {
background: #fff;
}
.btn {
display: inline-block;
padding: 4px 12px;
margin-bottom: 0;
font-size: 14px;
line-height: 20px;
text-align: center;
vertical-align: middle;
cursor: pointer;
box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05);
border-radius: 4px;
}
.btn-danger {
color: #fff;
background-color: #da4f49;
border: 1px solid #bd362f;
}
.btn-danger:hover {
color: #fff;
background-color: #bd362f;
}
.btn:focus {
outline: none;
}
import Vue from 'vue'
import App from './components/App.vue'
import store from './store'
import './base.css'
new Vue({
el: '#app',
components: {App},
// template: ' ',
render: h => h(App),
store
})