淘宝联盟怎么自己做网站推广,免费查企业信息查询,开发公司冬季安全生产工作方案,注册公司最少需要多少钱Vuex
2.1 什么是Vuex Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式。 Vuex在组件之间共享数据。 2.2 使用 vue cli 构建项目
2.3 入门案例
2.3.1 定义数据
export default new Vuex.Store({state: { // 状态区域#xff08;定义变量区域#xff09;user: ,toke…Vuex
2.1 什么是Vuex Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式。 Vuex在组件之间共享数据。 2.2 使用 vue cli 构建项目
2.3 入门案例
2.3.1 定义数据
export default new Vuex.Store({state: { // 状态区域定义变量区域user: ,token: },mutations: { //修改变量setUser(state, value) {state.user value},setToken(state, value){state.token value}},2.3.2 调用数据
获得数据调用state区域和计算属性结合使数据可以实时更新
templatediv{{token}}/div
/templatescript
export default {computed: { // 计算属性token() {return this.$store.state.token}},
}/scriptstyle
/style设置数据调用mutations区域
templatediv{{token}} br/input typebutton value切换数据 clicksetToken(tom)/div
/templatescript
export default {computed: { // 计算属性token() {return this.$store.state.token}},methods: {setToken(value) {this.$store.commit(setToken,value)}},
}
/scriptstyle/style2.4 总结
属性描述实例state用于定义对象的状态 需要共享的数据获得方式 this.$store.state.usernamestate: { username: ‘jack’, password: ‘6666’ }gettersvuex的计算属性获得依赖的缓存数据只有依赖的值发生改变才重新计算 获得方式 this.$store.getters.getUsernamegetters: { getUsername(state) { return state.username } }mutations唯一可以修改对象状态的方式 修改方式 this.$store.commit(‘setUsername’,‘肉丝’)mutations: { setUsername(state,value){ state.username value } }actions可以执行mutationsaction运行有异步操作 执行方式 this.$store.dispatch(‘uesrnameAction’,‘肉丝666’)actions: { //事件区域 uesrnameAction(context,value){context.commit(‘setUsername’,value) } }modulevuex的模块允许有独立的以上4个属性
2.5 高级全局引用(映射)
在vuex中提供一组map用于简化vuex的操作
2.5.1 入门
步骤一导入对应map
//1 导入需要map
import {mapState} from vuex步骤二获得数据
templatediv{{token}} br/{{user}} br//div
/templatescript
//1 导入需要map
import {mapState} from vuex
/*1. mapState() vuex中定义函数2. 通过数组参数确定从vuex中获得哪些变量的值mapState([token,user])此函数返回如下{token() {return this.$store.state.token},user() {return this.$store.state.user}}3. { { } } 此语法是错误需要将 mapState函数 返回对象抽取即只要内容...mapState([token,user])返回的内容如下token() {return this.$store.state.token},user() {return this.$store.state.user}
*/
export default {computed: {...mapState([token,user])},
}
/scriptstyle/style步骤三设置数据
templatediv{{token}} br/{{user}} br/input typebutton value切换数据 clicksetToken(tom)/div
/templatescript
//1 导入需要map
import {mapState,mapMutations} from vuex
export default {computed: {...mapState([token,user])},methods: {...mapMutations([setToken])},
}/scriptstyle/style2.5.2 完整参考
编写store.js
import Vue from vue
import Vuex from vuexVue.use(Vuex)export default new Vuex.Store({state: { //存储数据name : jack},mutations: { //修改数据changeName(state , value){state.name value}},actions: { //触发mutationchangeNameFn(content, value){content.commit(changeName, value);}},getters: { //获得数据getName: state state.name,getNameLen : (state, getters) getters.getName.length}
})About.vue
templatediv classabouth1This is an about page/h1!-- 1.4 显示改变后的数据 --{{showName}} br/!-- 2.2 显示计算属性映射到的“属性别名” --{{showName2}} br/!-- 3.2 显示计算属性映射到的“默认属性别名” --{{name}} br/input typetext placeholder请输入存储的数据 v-modelusername value br/input typebutton value点击切换 clicknameclick /div
/templatescriptimport {mapState,mapMutations,mapActions,mapGetters} from vuex
export default {data() {return {username : //绑定数据}},methods: {nameclick(){//this.$store.commit(changeName, this.username ); //提交数据用于修改vuex中的数据//this.$store.dispatch(changeNameFn , this.username);this.changeName(this.username); //1.2 执行映射后的新函数 this.changeName()console.info(this.$store.getters.getName)},...mapMutations([changeName]) //1.1 将 this.changeName() 映射为 this.$store.commit(changeName)},computed: {showName(){return this.$store.state.name; //1.3 显示vuex中的数据通过“计算属性”实时显示},...mapState({showName2 : state state.name //2.1 将 this.showName2 映射为 this.$store.state.name}),...mapState([name]) , //3.1 将 this.name 映射为 this.$store.state.name...mapGetters([getName]) //4.1 将 this.getName 映射为 this.$store.getters.getName},}/script2.6 流程总结 步骤一package.json 确定vuex版本 自动生成 vuex: ^3.0.1步骤二vuex配置文件src/store.js 自动生成 import Vue from vue
import Vuex from vuexVue.use(Vuex)export default new Vuex.Store({state: { //数据存储区域},mutations: { //函数声明区域修改数据},actions: { //事件区域执行函数}
})步骤三main.js中配置vuex 自动生成