由于 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-全局状态管理【传递参数】的更多相关文章

  1. 理解vuex的状态管理模式架构

    理解vuex的状态管理模式架构 一: 什么是vuex?官方解释如下:vuex是一个专为vue.js应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证以一种可预测的 ...

  2. 小程序全局状态管理,在页面中获取globalData和使用globalSetData

    GitHub: https://github.com/WozHuang/mp-extend 主要目标 微信小程序官方没有提供类似vuex.redux全局状态管理的解决方案,但是在一个完整的项目中各组件 ...

  3. 微信小程序全局状态管理 wxscv

    微信小程序中,数据状态不同页面中不能跨页面同步更新,也就是缺失类似vuex,mobx,redux全局的数据状态管理功能. 有些人移植了这些库,但是毕竟不是微信小程序生态的东西. Tencent也发布了 ...

  4. Vuex实现状态管理

    Vuex使用总结 1 Vuex简介 Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式,Vuex抽取了各个组件的共享部分,以全局单例模式进行状态的管理.在原生vue中各个组件之间传值使用的 ...

  5. vuex vue状态管理

    第一步安装vuex(安装在生产环境) npm install vuex 第二步 src下新建store文件夹 用来专门放状态管理,store文件夹下新建四个js文件 index.js  actions ...

  6. Vuex,状态管理模式

    对于 Vue 本人目前接触不深,只得浅层分析,Vue 是单向数据流, state,驱动应用的数据源: view,以声明方式将 state 映射到视图: actions,响应在 view 上的用户输入导 ...

  7. vue2.0 仿手机新闻站(三)通过 vuex 进行状态管理

    1.创建 store 结构 2.main.js  引入 vuex 3. App.vue  组件使用 vuex <template> <div id="app"&g ...

  8. Vuex.js状态管理共享数据 - day8

    VScode文件目录: amount.vue代码如下: <template> <div> <!-- <h3>{{ $store.state.count }}& ...

  9. vuex的状态管理模式

    1.store.js Vuex 通过 store 选项,提供了一种机制将状态从根组件“注入”到每一个子组件中(需调用 Vue.use(Vuex)) state:存放数据. mutations:提交状态 ...

  10. 举个例子去理解vuex(状态管理),通俗理解vuex原理,通过vue例子类比

    通俗理解vuex原理---通过vue例子类比   本文主要通过简单的理解来解释下vuex的基本流程,而这也是vuex难点之一. 首先我们先了解下vuex的作用vuex其实是集中的数据管理仓库,相当于数 ...

随机推荐

  1. C#异常操作

    C#异常处理子系统包括: Try:需要异常机制的函数在其中运行 Catch:捕获异常 Throw:抛出异常 Finally:在try结束实现 C#异常主要在Exception类中,而在CLR机制中的异 ...

  2. Python学习之==>集合

    1.简介 集合也是一种数据类型,一个类似列表东西,它的特点是无序的,不重复的,也就是说集合中是没有重复数据的. 2.集合的作用 它可以把一个列表中重复的数据去掉,而不需要你再写判断 可以做关系测试,比 ...

  3. 未解决:found 1 high severity vulnerability run `npm audit fix` to fix them, or `npm audit` for details

    问题出现: 在通过 `ng new hello-world` 命令新建项目时,项目出现以下警告: found high severity vulnerability run `npm audit fi ...

  4. django连接数据库的类型

    字段类型 django的models里面字段类型除了上面的常用的 models.CharField和models.IntegerField,还有更多的类型 1.models.AutoField 自增列 ...

  5. django F和Q 关键字使用

    F 的使用: 想给表里每个价格加上一百就要用上F,直接加是不行的.

  6. Hive-生成一个大文件(小文件合并)

    set hive.execution.engine=mr; --在 map-reduce 作业结束时合并小文件.如启用,将创建 map-only 作业以合并目标表/分区中的文件. set hive.m ...

  7. [Web 前端] 024 js 的定时器及函数

    1. Javascript 定时器 1.1 计时事件 设定一个间隔,时间到了后准时执行代码,此为"计时事件" 1.2 作用 1.制作动画 2.异步操作 1.3 定时器的类型及语法 ...

  8. [转帖]虚拟内存探究 -- 第一篇:C strings & /proc

    虚拟内存探究 -- 第一篇:C strings & /proc http://blog.coderhuo.tech/2017/10/12/Virtual_Memory_C_strings_pr ...

  9. QButtonGroup

    单选按钮和多选按钮,存放进QButtonGroup中 QButtonGroup方法来实现分组:将相同功能的按键,设为一个分组,然后可以进行 单选 或 多选 或 互斥单选 QAbstractButton ...

  10. [Codeforces 1246B] Power Products (STL+分解质因数)

    [Codeforces 1246B] Power Products (STL+分解质因数) 题面 给出一个长度为\(n\)的序列\(a_i\)和常数k,求有多少个数对\((i,j)\)满足\(a_i ...