vuex核心内容及重点细节总结
文章目录
文章目录
https://xueqiu.com/4437336987...
https://xueqiu.com/4437336987...
https://xueqiu.com/4437336987...
https://xueqiu.com/4437336987...
https://xueqiu.com/4437336987...
https://xueqiu.com/4437336987...
https://xueqiu.com/4437336987...
https://xueqiu.com/4437336987...
https://xueqiu.com/4437336987...
https://xueqiu.com/4437336987...
https://xueqiu.com/4437336987...
https://xueqiu.com/4437336987...
https://xueqiu.com/4437336987...
https://xueqiu.com/4437336987...
https://xueqiu.com/4437336987...
https://xueqiu.com/4437336987...
https://xueqiu.com/4437336987...
https://xueqiu.com/4437336987...
https://xueqiu.com/4437336987...
https://xueqiu.com/4437336987...
https://xueqiu.com/4437336987...
https://xueqiu.com/4437336987...
https://xueqiu.com/4437336987...
https://xueqiu.com/4437336987...
https://xueqiu.com/4437336987...
https://xueqiu.com/4437336987...
https://xueqiu.com/4437336987...
https://xueqiu.com/4437336987...
https://xueqiu.com/4437336987...
https://xueqiu.com/4437336987...
https://xueqiu.com/4437336987...
https://xueqiu.com/4437336987...
https://xueqiu.com/4437336987...
https://xueqiu.com/4437336987...
https://xueqiu.com/4437336987...
https://xueqiu.com/4437336987...
https://xueqiu.com/4437336987...
https://xueqiu.com/4437336987...
https://xueqiu.com/4437336987...
https://xueqiu.com/4437336987...
vuex简介


vuex的状态管理



vuex几大核心内容

store文件夹的index.js文件 使用vuex的步骤
state模块

state就是保存一些变量,所有vue的组件都能共享
`// 保存状态
state: {
counter:1000,
students:[
{id:110,name:'lizhi',age:18},
{id:111,name:'theshy',age:22},
{id:112,name:'zouxinxin',age:38},
],
info:{
name:"caixukun",
hobbis:['rap','basketball'],
age:22
}
},`
* 1
* 2
* 3
* 4
* 5
* 6
* 7
* 8
* 9
* 10
* 11
* 12
* 13
* 14
mutations模块
mutations怎么传递参数

mutations提交风格

mutations响应规则

mutations常量类型细节编程


mutations同步函数

getters模块

Actions模块

module模块


详细代码
store/index.js
`import Vue from 'vue'
import Vuex from 'vuex'
import {
INCREMENT
} from './mutations-types'
// 1,安装插件
Vue.use(Vuex)
// 2,创建并导出对象
// 2.1创建module对象
const moduluA = {
state: {
name: 'Sutter'
},
mutations: {
UpdateName(state,payload){
state.name = payload
}
},
actions: {
aUpdateName(context){
setTimeout(()=>{
context.commit('UpdateName','wangf')
},1000)
}
},
getters: {
fullname(state){
return state.name+ '11111'
},
fullName(state,getters){
return getters.fullname + '2222'
},
newFullname(state,getters,rootState){
return getters.fullName+rootState.info.name
}
}
}
export default new Vuex.Store({
// 保存状态
state: {
counter:1000,
students:[
{id:110,name:'lizhi',age:18},
{id:111,name:'theshy',age:22},
{id:112,name:'zouxinxin',age:38},
],
info:{
name:"caixukun",
hobbis:['rap','basketball'],
age:22
}
},
// mutation 转变
mutations: {
// 方法 默认参数state
[INCREMENT](state){
state.counter++
},
decreament(state){
state.counter--
},
increCount(state,payload){
console.log(payload);
state.counter += payload.count
},
addStudent(state,student){
state.students.push(student)
},
updateInfo(state) {
Vue.set(state.info,'address','beijing') //添加属性
Vue.delete(state.info,'age') //删除属性
// 错误的代码,不能在这里进行异步操作
// setTimeout(()=>{
// state.info.name = Sutter
// },1000)
state.info.name="Sutter"
},
},
actions: {
// context上下文编程翻译
// aUpdateInfo(context,payload){
// setTimeout(()=>{
// context.commit('updateInfo')
// // console.log(payload);
// console.log(payload.param);
// payload.successed()
// },1000)
// },
aUpdateInfo(context,payload){
return new Promise((resolve,reject)=>{
setTimeout(()=>{
context.commit('updateInfo')
console.log(payload);
resolve("action.params")
},1000)
})
}
},
getters: {
powerCounter(state){
return state.counter*state.counter
},
moreTwenty(state,getters){
return state.students.filter(s =>{
return s.age>getters.moreAge
})
},
moreTwentyLen(state,getters){
return getters.moreTwenty.length
},
moreAge(state){
// return function(age){
// return state.students.filter(s =>{
// return s.age>age
// })
// }
return age =>{
return state.students.filter(s => s.age >age)
}
}
},
modules: {
a: moduluA
}
})`
* 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
* 129
* 130
* 131
* 132
* 133
* 134
* 135
* 136
* 137
HelloWorld.vue
`<template>
<div class="hello">
<h2>module中内容</h2>
<h2>{{$store.state.a.name}}</h2>
<button @click="updateName('paul')">修改名字</button>
<h2>{{$store.getters.fullname}}</h2>
<h2>{{$store.getters.fullName}}</h2>
<h2>{{$store.getters.newFullname}}</h2>
<button @click="asyncName()">异步修改</button>
<h2>---------------------------------</h2>
<h2>{{$store.state.counter}}</h2>
<button @click="add()">+</button>
<button @click="reduce()">-</button>
<button @click="addCount(5)">+5</button>
<button @click="addStu({id:111,name:'123',age:12})">添加学生</button>
<h2>-------------------------------------</h2>
<h2>{{$store.getters.powerCounter}}</h2>
<h2>--------------</h2>
<h2>{{$store.getters.moreTwentyLen}}</h2>
<h2>---------------</h2>
<h2>{{$store.state.info}}</h2>
<button @click="updateInfo">添加信息</button>
</div>
</template>
<script>
import {
INCREMENT
} from '../store/mutations-types'
export default {
name: 'HelloWorld',
props: {
msg: String
},
// computed:{
// moreTwenty(){
// return this.$store.state.students.filter(s =>{
// return s.age>20
// })
// }
// },
methods: {
add(){
// 拿到store对象进行提交commit
console.log(INCREMENT);
this.$store.commit(INCREMENT)
},
reduce(){
this.$store.commit('decreament')
},
addCount(num){
// payload 负载
// 普通的提交风格
// this.$store.commit('increCount',num)
// 特殊的提交风格
this.$store.commit({
type:'increCount',//事件类型.
count:num
})
},
addStu(student){
this.$store.commit('addStudent',student)
},
// updateInfo(){
// // this.$store.commit('updateInfo')
// this.$store.dispatch('aUpdateInfo',{
// param:"我是参数",
// successed(){
// console.log("传入成功");
// }
// })
// }
updateInfo(){
// this.$store.commit('updateInfo')
this.$store
.dispatch('aUpdateInfo','携带的params')
.then(res=>{
console.log("里面完成了回调");
console.log(res);
})
},
updateName(param){
this.$store.commit('updateName',param)
},
asyncName(){
this.$store.dispatch('aUpdateName')
}
},
}
</script>
<style scoped>
</style>`
* 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
App.vue
<template>
<div id="app">
<h2>{{$store.state.counter}}</h2>
<h2>{{$store.getters.moreAge(0)}}</h2>
<!-- <h2>{{$store.getters.moreAge(10)}}</h2> -->
<h2>---------------------------------</h2>
<HelloWorld></HelloWorld>
</div>
</template>
<script>
import HelloWorld from './components/HelloWorld'
export default {
name:"app",
components:{
HelloWorld
}
}
</script>
<style>
</style
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论