vuex的安装 npm i vuex -s
npm i vuex - s
引入和配置
在src目录下新建 store文件夹 在store文件夹下新建 index. js 即:src/ store/ index. js
import Vue from 'vue'
import Vuex from 'vuex'
Vue. use ( Vuex)
const store = new Vuex. Store ( {
state: {
name: '赵丽颖'
}
} )
export default store
在main. js中引入该文件
import Vue from 'vue'
import App from './App'
import router from './router'
import * as filters from './filters'
import store from './store'
Vue. config. productionTip = false
Object. keys ( filters) . forEach ( key => { Vue. filter ( key, filters[ key] ) } )
new Vue ( {
el: '#app' ,
router,
store,
components: { App } ,
template: '<App/>'
} )
state数据中的调用
模板调用
< template>
< div>
< div>
明星姓名: { { $store. state. name} }
< / div>
< / div>
< / template>
js中调用
console. log ( this . $store. state. name)
state值得修改 通过mutations进行调用 使用this.$store.commit(方法名,值) 同步时使用
注:不要直接进行修改state得值,需要通过mutations或者Vue. set ( ) 或者 this . $set ( ) 进行修改
同步时调用,异步使用Actions
import Vue from 'vue'
import Vuex from 'vuex'
Vue. use ( Vuex)
const store = new Vuex. Store ( {
state: {
name: '赵丽颖'
} ,
mutations: {
nameEdit ( state, val = '' ) {
state. name = val
}
}
} )
export default store
调用
this . $store. commit ( 'nameEdit' , '赵文卓' )
console. log ( this . $store. state. name)
this.$set设置/更改 或者 this.$delete删除 state内的值
this . $set ( this . $store. state, 'age' , 32 )
console. log ( this . $store. state. age)
this . $set ( this . $store. state, 'age' , 23 )
console. log ( this . $store. state. age)
this . $delete ( this . $store. state, 'age' )
console. log ( this . $store. state. age)
getters 用于将属性值处理后返回
语法:
getters: {
func1 ( state) {
return state. 属性值 + 其它的信息等
} ,
func2 ( state, getters) {
return getters. func1 + state. 属性值 + 其它额信息等
}
}
参数
第一个参数即state
第二个参数即getters本身,也就是说getters下的方法可以随时调用其它的getters方法
import Vue from 'vue'
import Vuex from 'vuex'
Vue. use ( Vuex)
const store = new Vuex. Store ( {
state: {
name: '赵丽颖' ,
school: '中南大学'
} ,
mutations: {
nameEdit ( state, val = '' ) {
state. name = val
}
} ,
getters: {
nameInfo ( state) {
return ` ${ state. name} 是个大明星`
} ,
fromInfo ( state, getters) {
return ` ${ getters. nameInfo} ,并且毕业于 ${ state. school} `
}
}
} )
export default store
console. log ( this . $store. getters. nameInfo)
console. log ( this . $store. getters. fromInfo)
Actions 的异步调用mutations修改state内的值
actions: {
func1 ( content, 其它参数) {
return new Promise ( resolve => {
content. commit ( mutations中的参数, 其他参数)
resolve ( )
} )
}
}
import Vue from 'vue'
import Vuex from 'vuex'
Vue. use ( Vuex)
const store = new Vuex. Store ( {
state: {
name: '赵丽颖' ,
school: '中南大学'
} ,
mutations: {
nameEdit ( state, val = '无名氏' ) {
state. name = val
}
} ,
getters: {
nameInfo ( state) {
return ` ${ state. name} 是个大明星`
} ,
fromInfo ( state, getters) {
return ` ${ getters. nameInfo} ,并且毕业于 ${ state. school} `
}
} ,
actions: {
setName ( content, val = '无名氏' ) {
console. log ( content)
return new Promise ( resolve => {
content. commit ( 'nameEdit' , val)
resolve ( )
} )
}
}
} )
export default store
调用 this . $store. dispacth ( 'setName' , 其他参数)
console. log ( this . $store. state. name)
await this . $store. dispatch ( 'setName' , '王富贵' )
console. log ( this . $store. state. name)
如果Actions方法中只需要使用到mutations中的方法,则可以直接在接参的时候解构处{ commit} ,在方法中直接使用commit ( )
import Vue from 'vue'
import Vuex from 'vuex'
Vue. use ( Vuex)
const store = new Vuex. Store ( {
state: {
name: '赵丽颖' ,
school: '中南大学'
} ,
mutations: {
nameEdit ( state, val = '无名氏' ) {
state. name = val
} ,
schoolEdit ( state, val = '未知大学' ) {
state. school = val
}
} ,
getters: {
nameInfo ( state) {
return ` ${ state. name} 是个大明星`
} ,
fromInfo ( state, getters) {
return ` ${ getters. nameInfo} ,并且毕业于 ${ state. school} `
}
} ,
actions: {
setName ( content, val = '无名氏' ) {
console. log ( content)
return new Promise ( resolve => {
content. commit ( 'nameEdit' , val)
resolve ( )
} )
} ,
setSchool ( { commit} , val = '未知大学' ) {
return new Promise ( resolve => {
commit ( 'schoolEdit' , val)
resolve ( )
} )
}
}
} )
export default store
console. log ( this . $store. state. school)
await this . $store. dispatch ( 'setSchool' , '北京大学' )
console. log ( this . $store. state. school)
扩展运算符对state等的调用方式 …mapState() …mapGetters() …mapMutations() …mapActions()
语法:
import { mapState, mapGetters, mapMutations, mapActions} from 'vuex'
... mapState ( { 别名: '原名' , 别名: '原名' } )
this . 别名
... mapGetters ( 别名: '原名' , 别名: '原名' )
this . 别名
... mapMutations ( 别名: '原名' , 别名: '原名' )
this . 别名 ( 替换的值)
... mapActions ( 别名: '原名' , 别名: '原名' )
await this . 别名 ( 替换的值)
< ! -- -- >
< template>
< div>
< / div>
< / template>
< script>
import { mapState, mapGetters, mapMutations, mapActions} from 'vuex'
export default {
name: 'VueBase' ,
data ( ) {
return {
}
} ,
computed: {
... mapState ( {
storeName: 'name' ,
storeSchool: 'school'
} ) ,
... mapGetters ( {
storeNameInfo: 'nameInfo'
} )
} ,
filters: {
} ,
methods: {
... mapMutations ( {
storeNameEdit: 'nameEdit'
} ) ,
... mapActions ( {
actionSetSchool: 'setSchool'
} )
} ,
async created ( ) {
console. log ( this . storeName)
console. log ( this . storeSchool)
console. log ( this . address)
console. log ( this . storeNameInfo)
this . storeNameEdit ( '张朝阳' )
console. log ( this . storeName)
await this . actionSetSchool ( '清华大学' )
console. log ( this . storeSchool)
} ,
mounted ( ) {
}
}
< / script>
< style scoped>
< / style>