Vue入门笔记三(Vuex)

摘要:
导入Vuefrom'vu'从'vuex'vue导入vuex。使用(Vuex)2。使用Vuex.Store构造函数创建存储:状态是共享数据的唯一数据源。状态是只读的(1)在store选项中添加状态函数,conststore=newVuexStore({state(){return{user:

《Vue.js项目实战》

Vuex

集中式的状态管理

  • Vuex从Flux(由Facebook开发)的概念中获取得灵感。Flux又一系列指导原则构成,阐明了如何使用集中式store来实现组件之间的单向数据流。
  • Vuex的核心元素是store,是进行数据存储和数据处理的主要架构。
    store包含如下信息:
state            // 存储应用状态的响应式数据对象
getter          // 等价于store的计算属性
mutation     // 用来改变应用状态的函数
action         // 通常用来调用异步API的函数,然后使用mutation改变数据

Vuex store

1.安装vuex

npm i -S vuex

然后新建store文件夹,并创建index.js来安装Vuex插件:

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

2.使用Vuex.Store构造函数创建store:

const store = new Vuex.Store({
    // TODO 选项
})

3.导出store:

export default store

4.在main.js文件中,导入store:

import store from './store'

5.为了让store在应用中生效,我们还需要像注入路由器一样注入它:

const app = new Vue({
    router,
    store,
    el: '#app',
    ...App,
  })

state是唯一数据源

state是store的主要组成部分,它展示了应用中组件的所有共享数据。

  • state是共享数据的唯一数据源
  • state是只读的
    (1)在store选项中添加state函数,该函数返回一个对象:
const store = new Vuex.Store({
  state () {
    return {
      user: null
    }
  },
})

(2)在组件中读取状态:

this.$store.state.user

使用mutation修改状态

  • mutation是修改state的唯一方式
  • mutation接受state作为第一个参数,同时接受一个可选的载荷(payload)参数,以此来更新state
  • 不应该在mutation中使用异步操作
  • 严格模式:避免在mutation中使用异步函数strict: true
  • 不要在生产环境下启用严格模式,以免影响性能strict: process.env.NODE_ENV !== 'production'
    (1)添加一个mutation:
const store = new Vuex.Store({
    state () { /* ... */ },
    mutations: {
        user: (state, user) => {
            state.user = user
        }
    }
})

(2)表明调用mutation的用词是submit,在组件中:

this.$store.commit('user', userData)

使用getter计算和返回数据

(1)创建一个getter:

const store = new Vuex.Store({
    // ...
    getter: {
        user: state => state.user,
    }
})

(2)在组件中,可以用这个getter代替之前直接获取状态的方法:

this.$store.getter.user // this.$store.state.user

使用action操作store

  • action不仅可以提交mutation,还能做异步操作
  • action的声明由一个类型和一个处理函数构成store.dispath('action-type', payloadObject)
  • action的处理函数接收两个参数:
    1.context
    2.payload
    (1)创建action:
actions: {
    login ({ commit }) {
      const userData = {
        profile: {
          displayName: 'Mr Cat',      
        },
      }
     commit('user', userData)
    },
    // ...
},

(2)在组件中使用:

this.$store.dispatch('login')

辅助函数

Vuex提供了一系列辅助函数供添加state、getter、mutation以及action。出于将组建中的状态和逻辑分离的考虑,我们只应该在组件中使用getter和action,所以只会用到mapGetters和mapActions。

辅助函数的参数可以是:
1.类型的数组,其中的每一个元素对应于组件中的同名数据
2.对象,其中的键是组件中数据的别名,值则是类型
数组语法例如:

mapGetters(['a', 'b'])
// 等价于
{
    a () { return this.$store.getters.a },
    b () { return this.$store.getters.b },
}

对象语法例如:

mapGetters({ x: 'a', y: 'b' })
// 等价于
{
    x () { return this.$store.getters.a },
    y () { return this.$store.getters.b },
}

(1)在组件中导入:

import { mapGetters, mapActions } from 'vuex'

(2)然后将组件修改为:


export default {
  computed: {
    ...mapGetters([
      'user',
      'userPicture',
    ]),
  },

  methods: {
    ...mapActions({
        centerOnUser: 'login',
        logout: 'logout'
    }),
  },

}

Vuex模块

创建两个模块:

  • maps
  • posts
    (1)在store文件夹中新建一个maps.js文件。它导出一个默认模块定义,其中包括地图的state:
export default {
    namespaced: true,

    state () {
        return {
            center: {
                lat: 48.8538302,
                lng: 2.2982161,
            }
        }
    }

}

(2)将模块添加到store中,在store/index.js文件中添加modules选项:

import maps from './maps'

const store = new Vuex.Store({
    // ...
    modules: {
        maps,
    }
})

默认情况下,模块中getter、mutation、action的状态也会成为这个模块的状态。这里它是store.state.maps。

带命名空间的模块

上面的namespaced选项告诉Vuex在该模块的所有getter、mutation、action前添加maps/命名空间,还会在这个模块内的commit和dispatch调用中添加它们。

// getter模块
getters: {
    center: state => state.center,
}

maps/center getter 会被添加到store中。使用时可以这么写:
1.getters

this.$store.getters['maps/center']

2.使用getter辅助函数:

mapGetters({
    center: 'maps/center',
})

3.可以指定命名空间:

mapGetters('maps', [
    'center'
])

4.使用createNamespacedHelpers方法生成基于某个命名空间的辅助函数:

import { createNamespacedHelpers } from vuex
import { mapGetters } = createNamespacedHelpers('maps')

export default {
    computed: mapGetters([
        'center',
    ])
}

访问全局元素

1.在getter中
你可以在命名空间模块的getter中访问到根状态和根getter(即所有的getter):

someGetter: (state, getters, rootState, rootGetters) => { /* ... */}

2.在action中
你可以访问到上下文的rootGetters,还可以在commit和dispatch调用中使用{ root: true }选项:

myAction({ dispatch, commit, getters, rootGetters }) {
    getters.a // store.getters['maps/a']
    rootGetters.a // store.getters['a']
    commit('someMutation') // 'maps/someMutation'
    commit('someMutation', null, { root: true }) // 'someMutation'
    dispatch('someAction') // 'maps/someAction'
    dispatch('someAction', null, { root: true }) // 'someAction'
}

免责声明:文章转载自《Vue入门笔记三(Vuex)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Python学习之JSON格式的序列化和反序列化腾讯蓝鲸cmdb部署下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

layer iframe 设置关闭按钮 和刷新和弹出框设置

layer弹出层的关闭问题    就是在执行添加或修改的时候,需要将数据提交到后台进行处理,这时候添加成功之后最理想的状态是关闭弹出层并且刷新列表的数据信息,之前一直想实现这样,可一直没有成功,今天决定好好弄一弄,在仔细看过layer的帮助手册以及查阅资料之后,有了以下的解决办法: 一、关闭弹出窗   这是layer官网给出的帮助手册,讲解的比较详细...

vue SSR 部署详解

 先用vue cli初始化一个项目 输入命令行开始创建项目: vue create my-vue-ssr 记得不要选PWA,不知为何加了这个玩意儿就报错。 后续选router模式记得选 history 模式。 项目就绪后,cd 进入,开始改造。 先贴项目结构: 改造main.js、router.js和store.js 根据vue ssr官方文档进行路由...

PAT 1014. 福尔摩斯的约会 (20)

大侦探福尔摩斯接到一张奇怪的字条:“我们约会吧! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk d&Hyscvnm”。大侦探很快就明白了,字条上奇怪的乱码实际上就是约会的时间“星期四 14:04”,因为前面两字符串中第1对相同的大写英文字母(大小写有区分)是第4个字母'D',代表星期四;第2对相同的...

python——pandas初体验

一、pandas简介 Pandas是面向数据分析场景设计的Python开源软件工具包,其名字来自英文词组panel data,作为经济界的术语指多维结构化的数据集。从命名来看,Pandas特别适合处理序列数据、表格数据等具有良好结构的数据。在软件使用上,由于Pandsa是基于BSD开源软件许可证发布的,能够很方便地在学习、办公和工业应用等场合使用。 二、p...

numpy和matplotlib读书笔记

1.numpy笔记:获取numpy.array中最后一列的数据1.y = to_nparray[:,-1] # 最后一列2.X = to_nparray[:,0:-1] # 从第一列开始到倒数第二列numpy.dtype:int转字符串1.# np.dtype: int转字符串2.res = np.char.mod('%d', res)numpy拼接字符串...

unittest自定义封装与应用

和大家分享一个自己二次封装uniitest的方法,大家可以在评论区多多指教,一起分享学习; 一、unittest基类封装 import osimport unittestfrom common.log_print import Logfrom common.get_config import getconfigfrom common.base_page_i...