Vuex-全局状态管理【传递参数】
由于 async 异步获取数据,加载页面时,页面会感觉一闪而过的抖动,如果想页面不闪,推荐使用 SSR 【vuex】,由服务端 渲染SSR页面出来。此话记于 2020年01月08号,项目nuxt.js 构建 Vue+Koa+MongoDB 全栈美团项目
src根目录
新建store文件夹,新建index.js 作为入口
在store文件夹中 新建modules文件夹
modules文件夹中,新建 a.js b.js 2个文件
a.js
const state = {
money: 10
}
const mutations = {
// 我这里收到 参数以后,操作state.money的值改变
add(state, param) {
state.money += param
},
reduce(state) {
state.money--
}
}
const actions = {
//这里先接收到参数,我在驱动mutation里的add方法,同时传入参数
Actions_add: ({ commit }, param) => {
commit('add', param)
},
Actions_reduce: ({ commit }) => {
commit('reduce')
}
}
export default {
namespaced: true,//命名空间模块
state,
mutations,
actions
}
b.js
const state = {
count: 1
}
const mutations = {
add(state){
state.count++
},
reduce(state) {
state.count--
}
}
const actions = {
Actions_add:({commit})=>{
commit('add')
},
Actions_reduce:({commit})=>{
commit('reduce')
}
}
export default {
namespaced:true,//命名空间模块
state,
mutations,
actions
}
index.js
import Vue from 'vue'
import Vuex from 'vuex'
import money from './modules/a'
import count from './modules/b' Vue.use(Vuex) export default new Vuex.Store({
modules: {
money,
count
}
})
* 记得不要忘记在 main.js 中引入
import Vue from 'vue'
import App from './App.vue'
import store from './store/index'
// import store from './store'
Vue.config.productionTip = false new Vue({
store,
render: h => h(App)
}).$mount('#app')
在新建 a.vue \ b.vue 2个组件
a.vue
<template>
<div class="container">
<h1>Money</h1>
<hr />
<div class="input-group">
<span class="input-group-btn">
<button type="button" class="btn btn-success" @click="Actions_add(2)">增加2</button>
</span>
<input type="text" class="form-control" v-model="money.money" />
<span class="input-group-btn">
<button type="button" class="btn btn-danger" @click="Actions_reduce">减少</button>
</span>
</div>
</div>
</template> <script>
import { mapActions, mapState } from "vuex"
export default {
methods: {
...mapActions('money',["Actions_add", "Actions_reduce"])
},
computed: {
...mapState([
'money'
])
} }
</script> <style> </style>
b.vue
<template>
<div class="container">
<h1>Count</h1>
<hr />
<div class="input-group">
<span class="input-group-btn">
<button type="button" class="btn btn-success" @click="Actions_add">增加</button>
</span>
<input type="text" class="form-control" v-model="count.count" />
<span class="input-group-btn">
<button type="button" class="btn btn-danger" @click="Actions_reduce">减少</button>
</span>
</div>
</div>
</template> <script>
import { mapActions, mapState } from "vuex"
export default {
methods: {
...mapActions('count',["Actions_add", "Actions_reduce"])
},
computed: {
...mapState([
'count'
])
} }
</script> <style> </style>
效果图:

Vuex-全局状态管理【传递参数】的更多相关文章
- 理解vuex的状态管理模式架构
理解vuex的状态管理模式架构 一: 什么是vuex?官方解释如下:vuex是一个专为vue.js应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证以一种可预测的 ...
- 小程序全局状态管理,在页面中获取globalData和使用globalSetData
GitHub: https://github.com/WozHuang/mp-extend 主要目标 微信小程序官方没有提供类似vuex.redux全局状态管理的解决方案,但是在一个完整的项目中各组件 ...
- 微信小程序全局状态管理 wxscv
微信小程序中,数据状态不同页面中不能跨页面同步更新,也就是缺失类似vuex,mobx,redux全局的数据状态管理功能. 有些人移植了这些库,但是毕竟不是微信小程序生态的东西. Tencent也发布了 ...
- Vuex实现状态管理
Vuex使用总结 1 Vuex简介 Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式,Vuex抽取了各个组件的共享部分,以全局单例模式进行状态的管理.在原生vue中各个组件之间传值使用的 ...
- vuex vue状态管理
第一步安装vuex(安装在生产环境) npm install vuex 第二步 src下新建store文件夹 用来专门放状态管理,store文件夹下新建四个js文件 index.js actions ...
- Vuex,状态管理模式
对于 Vue 本人目前接触不深,只得浅层分析,Vue 是单向数据流, state,驱动应用的数据源: view,以声明方式将 state 映射到视图: actions,响应在 view 上的用户输入导 ...
- vue2.0 仿手机新闻站(三)通过 vuex 进行状态管理
1.创建 store 结构 2.main.js 引入 vuex 3. App.vue 组件使用 vuex <template> <div id="app"&g ...
- Vuex.js状态管理共享数据 - day8
VScode文件目录: amount.vue代码如下: <template> <div> <!-- <h3>{{ $store.state.count }}& ...
- vuex的状态管理模式
1.store.js Vuex 通过 store 选项,提供了一种机制将状态从根组件“注入”到每一个子组件中(需调用 Vue.use(Vuex)) state:存放数据. mutations:提交状态 ...
- 举个例子去理解vuex(状态管理),通俗理解vuex原理,通过vue例子类比
通俗理解vuex原理---通过vue例子类比 本文主要通过简单的理解来解释下vuex的基本流程,而这也是vuex难点之一. 首先我们先了解下vuex的作用vuex其实是集中的数据管理仓库,相当于数 ...
随机推荐
- sql 、linq、lambda 查询语句
http://www.cnblogs.com/lei2007/archive/2011/07/21/2113161.html
- 获取当前页面的title
#-*-coding:utf-8-*-from selenium import webdriverdriver = webdriver.Firefox()driver.get("https: ...
- Web Service自动化测试知识点导图
- linux/work
0.切换用户 //默认root用户是无固定密码的,并且是被锁定的,如果想给root设置一个密码 sudo passwd root //输入密码 & 确认密码 //切换root用户 su roo ...
- 判断RecyclerView是否滚动到底部
转:http://www.jianshu.com/p/c138055af5d2 一.首先,我们来介绍和分析一下第一种方法,也是网上最多人用的方法: public static boolean isVi ...
- mangde 的服务启动
启动命令如下
- Linux运维的第三周总结
01. 下列文件中, 包含了主机名到IP地址的映射关系的文件是() A. /etc/HOSTNAME B. /etc/hosts C. /etc/resolv.conf ...
- linux-memcache安装及memcached memcache扩展
linux memcached安装yum -y install libevent libevent-deve yum list memcached yum -y install memcached m ...
- Python 入门之流程控制语句
Python 入门之流程控制语句 1.if判断 (1) 单 if if –-如果 if 条件: 缩进 结果 (官方推荐4个空格,或者一个tab 不能空格和tab混合使用) money = 10 pri ...
- 管道(Pipe)----计算机进程间通信
参至他人博客:https://blog.csdn.net/u011583316/article/details/83419805