nuxt中使用Vuex

更新日期: 2020-05-26阅读: 2.5k标签: vuex

引言

在nuxt中使用vuex,以模块方式引用——计数器为例


目录结构



js模块写法

// user.js
// state为一个函数, 注意箭头函数写法
const state = () => ({
counter: 6
})

// mutations为一个对象
const mutations = {
increment(state) {
state.counter++
},
decrement(state) {
state.counter--
}
}
const actions = {

}
const getters = {

}
export default {
namespace: true, // 命名空间
state,
mutations,
actions,
getters
}

如果没有namespace,那么默认地每一个xxx.vue文件都会与store文件夹下的xxx.js相对应


vue写法

1. 直接获取

直接从user.js模块中获取state

<!-- user.vue -->
<div class="display-1">{{this.$store.state.user.count}}</div>


2. computed获取

用computed监听Vuex中state的变化, 及时渲染到界面上。如果在data中接收Vuex的state, 那么有可能监听不到state的变化[1], 一句话就是用computed接受Vuex的状态

computed介绍:

  1. 用于变量或方法的复杂逻辑, 如vue官网的反转字符串例子
  2. 相较于methods, computed有缓存机制, 相同的结果不会重复计算, 而methods中的方法是每次调用都会计算
// 从vuex中引入mapState
import { mapState } from 'vuex'
<!-- user.vue html部分 -->
<div class="display-1">{{counter}}</div>
<div class="display-1">{{tag}}</div>
// user.vue computed部分 第一种写法
computed:mapState('user', {
counter: state => state.counter // 注意写法,没中括号
}),
// user.vue computed部分 第二种写法, 普通函数
computed:mapState('user', {
counter: function(state) {
return state.counter
}
}),
// user.vue computed部分 第三种写法
computed:mapState("user", ['counter'])
// user.vue computed部分 第四种写法
// 方法与mapState共存
computed:{
tag(){ // 方法
return 'something'
},
...mapState('user', {
counter: function(state) {
return state.counter
}
}),
}

mapState({}|[])函数, 专门用来接收来自Vuex中的state, 接受一个对象或者一个数组,

...mapState()介绍:

因为mapState()不能直接写进computed对象中, 而computed的方法必须写进computed对象中, 所以为了让方法和state共存引入... 即...mapState()写法诞生

...为对象扩展符, 加上之后就可以在computed这个对象中与其他方法共存,没有方法时可以直接上第一、二种写法


触发mutations

// 触发mutations方式
this.$store.commit("mutationName", [parameter])
// mutations为一个对象
const mutations = {
increment(state) {
state.counter++
},
decrement(state) {
state.counter--
}
}


代码

index.vue中引用user.js模块

// index.vue
<template>
<div id="index">
<div class="display-1">
<b-icon icon="person"></b-icon>
<b-icon icon="person-fill"></b-icon>
<b-icon icon="triangle"></b-icon>
</div>
<div class="display-1">{{counter}}</div>
<div class="display-1">{{tag}}</div>
<div>
<b-button variant="outline-success" @click="increment">增加</b-button>
<b-button variant="outline-success" @click="decrement">减少</b-button>
</div>
</div>
</template>

<script>
import { mapState } from 'vuex'
export default {
// 初始化时触发mutations
fetch({ store }) {
store.commit('user/increment')
},
mounted() {},
data() {
return {}
},
methods: {
increment() {
this.$store.commit('user/increment')
},
decrement() {
this.$store.commit('user/decrement')
}
},
computed: {
tag(){
return 'something'
},
...mapState('user', {
counter: state => state.counter
})
},
components: {}
}
</script>

<style scoped>
#index {
min-height: 100%;
}
</style>

作者:@caseyfu
本文为作者原创,转载请注明出处:https://www.cnblogs.com/xfk1999/archive/2020/05/28/nuxt-use-vuex.html  


链接: https://www.fly63.com/article/detial/9203

为什么使用Vuex

在学习Vuex之前,先了解一下“单向数据流”。Vuex核心就是它的store,其中,有三个重要的部分,State:通过它存取多个组件共享的数据。Mutations:可以改变State中的数据,Actions:提交mutation,可以包含任意异步操作这一步不是必要的。

Vue.js学习,关于Vuex源码解析

Vuex是一个专门为Vue.js框架设计的、用于对Vue.js应用程序进行状态管理的库,它借鉴了Flux、redux的基本思想,将共享的数据抽离到全局,以一个单例存放,同时利用Vue.js的响应式机制来进行高效的状态管理与更新。

vue + vuex + koa2交互实例

主要目的是学会使用koa框架搭建web服务,从而提供一些后端接口,供前端调用。搭建这个环境的目的是: 前端工程师在跟后台工程师商定了接口但还未联调之前,涉及到向后端请求数据的功能能够走前端工程师自己搭建的http路径

vuex 源码:深入 vuex 之 mutation

vuex 规定更改 state 的唯一方法是提交 mutation,主要是为了能用 devtools 追踪状态变化。那么,提交 mutation 除了最主要的更改 state,它还做了其它一些什么事情呢,让我们来一探究竟。

使用Vuex解决Vue中的身份验证

Vuex为Vue.js应用管理状态.。对于应用中所有的组件来说,它被当做中央存储,并用规则确保状态只能以可预见的方式改变。对于经常检查本地存储来说,听起来是个更好的选择?让我们一起来探索下吧。

关于Vuex的action传入多个参数的问题

已知Vuex中通过actions提交mutations要通过context.commit(mutations,object)的方式来完成,然而commit中只能传入两个参数,第一个就是mutations,第二个就是要传入的参数

Vuex 的异步数据更新

更改 Vuex 的 store 中的状态的唯一方法是提交 mutation。Vuex 中的 mutation 非常类似于事件:每个 mutation 都有一个字符串的 事件类型 (type) 和 一个 回调函数 (handler)。这个回调函数就是我们实际进行状态更改的地方,并且它会接受 state 作为第一个参数 mutation 是同步执行,不是异步执行。

vue单页面应用刷新网页后vuex的state数据丢失的解决方案

其实很简单,因为store里的数据是保存在运行内存中的,当页面刷新时,页面会重新加载vue实例,store里面的数据就会被重新赋值。一种是state里的数据全部是通过请求来触发action或mutation来改变

Vuex状态管理,state、getter

首先在 vue 2.0+ 你的vue-cli项目中安装 vuex :然后 在src文件目录下新建一个名为store的文件夹,为方便引入并在store文件夹里新建一个index.js,里面的内容如下:接下来,在 main.js里面引入store,然后再全局注入一下

Vuex新手的理解与使用

开始尝试学习使用vue,是因为此前总是遇到页面逻辑数据与视图的一致性问题.在使用vue之前,我们使用jQuery插件的时候,一桩麻烦事就是既要在每个数据变更后,写代码去改变视图,又要考虑html上各种输入

点击更多...

内容以共享、参考、研究为目的,不存在任何商业目的。其版权属原作者所有,如有侵权或违规,请与小编联系!情况属实本人将予以删除!