vue-16-vuex

1, 介绍

对 vue 进行状态管理的, 集中存储所有组件的所有状态, 解决多个组件共享数据的问题.

即, 所有组件可以拿到同样的状态, 组件间共享数据

2, 在之前进行数据交互, 需要有子父关系

parent:

<template>
<div>
parent: {{ info }}
<Child :title="msg" @sendMsg="handlerMsg"/>
</div>
</template> <script>
import Child from "./Child";
export default {
name: "Parent",
components: {Child},
data() {
return {
msg: "hello son",
info: ""
}
},
methods: {
handlerMsg(info) {
this.info = info
}
}
}
</script> <style scoped> </style>

child:

<template>
<div>
Child:
receive from parent: {{ title }} <div>
<button @click="sendMsg">emit </button>
</div>
</div>
</template> <script>
export default {
name: "Child",
data() {
return { }
},
props: {
title: {
type: String,
default: ""
}
},
methods: {
sendMsg() {
this.$emit("sendMsg", "the message from son ")
}
}
}
</script> <style scoped>
</style>

3. vue的状态管理

view -> (dispath) Action -> (Commit)Mutations -> (Mutate) State -> View

注意: Actions不会必需品, 如果有异步操作才可能用到 Action, 否则不可以使用

4. 安装VueX

cnpm install --save vuex

5. 使用

// vuex
import Vuex from 'vuex'
Vue.use(Vuex)

6. store 是vuex 的核心

在src 下创建 store 文件夹, 并新建 index.js

写入store

// vuex
import Vue from 'vue'
import Vuex from 'vuex' Vue.use(Vuex) // 创建vuex的store
const store = new Vuex.Store({
state: {
count: 5
},
// 更改store的状态
mutations: {
increment (state) {
state.count++
},
decrement (state) {
state.count--
}
},
// 有异步的时候, 需要action
actions: {
increment(context) {
context.commit('increment')
},
decrement (context) {
setTimeout(function () {
context.commit("decrement")
}, 10)
}
},
// 通过getter 进行数据获取
getters: {
getState(state) {
return state.count > 0 ? state.count : 0;
}
}
}) export default store

在 main.js 中导入, 并注入到 App中

import store from './store'

Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
el: '#app',
// 注入 store
store,
router,
components: { App },
template: '<App/>',
})

7. 在vue 中进行引用

<template>
<div>
<div>
test...
{{ msg }}<br/>
</div> <div>
获取值 --
{{ getCount }} <br/>
<button @click="add">inc</button>
<button @click="des">decri</button>
</div> </div>
</template> <script>
export default {
name: "outter",
data() {
return {
msg: "hello"
} },
computed: {
// 避免编程负数, 需要通过方法进行获取
getCount() {
// return this.$store.state.count
return this.$store.getters.getState;
}
},
methods: {
add() {
this.$store.commit("increment")
},
des() {
// 使用 action中的异步方法
this.$store.dispatch("decrement")
},
}
}
</script> <style scoped> </style>

vue-15-vuex-store的用法的更多相关文章

  1. vue中vuex的五个属性和基本用法

    VueX 是一个专门为 Vue.js 应用设计的状态管理构架,统一管理和维护各个vue组件的可变化状态(你可以理解成 vue 组件里的某些 data ). Vuex有五个核心概念: state, ge ...

  2. 15.vue动画& vuex

    Vue.config.productionTip = false; ==是否显示提示信息== ==import/export== export xxx 必须跟跟对象或者和定义一起 对象: export ...

  3. [Nuxt] Add Arrays of Data to the Vuex Store and Display Them in Vue.js Templates

    You add array of todos to the store simply by adding them to the state defined in your store/index.j ...

  4. vue-devtools 获取到 vuex store 和 Vue 实例的?

    vue-devtools 获取到 vuex store 和 Vue 实例的? https://github.com/vuejs/vue-devtools       安装了 vue-devTools ...

  5. vue:vuex中mapState、mapGetters、mapActions辅助函数及Module的使用

    一.普通store中使用mapState.mapGetters辅助函数: 在src目录下建立store文件夹: ​ index.js如下: import Vue from 'vue'; import ...

  6. vue:vuex详解

    一.什么是Vuex? https://vuex.vuejs.org/zh-cn 官方说法:Vuex 是一个专为 Vue.js应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相 ...

  7. 一步一步教你用 Vue.js + Vuex 制作专门收藏微信公众号的 app

    一步一步教你用 Vue.js + Vuex 制作专门收藏微信公众号的 app 转载 作者:jrainlau 链接:https://segmentfault.com/a/1190000005844155 ...

  8. webpack4 + vue + vue-router + vuex

    ps: 所有案例使用的 node 及 npm 版本如下 node版本: v8.4.0 npm: 5.3.0 下一个案例默认是接着上一个继续写的 建议先熟悉以下文档 vue vue-router vue ...

  9. 应用四:Vue之VUEX状态管理

    (注:本文适用于有一定Vue基础或开发经验的读者,文章就知识点的讲解不一定全面,但却是开发过程中很实用的) 概念:Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应 ...

  10. vue + ts Vuex篇

    Vuex对Typescript的支持,仍十分薄弱,官方库只是添加了一些.d.ts声明文件,并没有像vue 2.5这样内置支持. 第三方衍生库 vuex-typescript, vuex-ts-deco ...

随机推荐

  1. 网页手机wap2.0网页的head里加入下面这条元标签,在iPhone的浏览器中页面将以原始大小显示,并不允许缩放

    网页手机wap2.0网页的head里加入下面这条元标签,在iPhone的浏览器中页面将以原始大小显示,并不允许缩放. <meta name="viewport" conten ...

  2. iBtais 多重嵌套循环

    iBatis支持集合循环, 但是如何做到双重循环, 请见下例子 例子描述: 需要去三张结构相同的表中获取信息, 需要将信息拼合去重后返回 入参数据类型: Map<String,Object> ...

  3. mongodb聚合操作

    1. mongodb的聚合是什么 聚合(aggregate)是基于数据处理的聚合管道,每个文档通过一个由多个阶段(stage)组成的管道,可以对每个阶段的管道进行分组.过滤等功能,然后经过一系列的处理 ...

  4. gitlab 之 升级、迁移

    -----故事背景- 公司服务器用vm装的虚拟机,由于公司服务器经常无故重启,且找不到原因,所以公司准备将vm迁移至Hyper-V,Hyper-V可以自启动虚拟机且免费. -----升级.迁移- 首先 ...

  5. Redis和memcached缓存技术

    缓存的定义:缓存就是在内存中存储的数据备份,当数据没有发生本质变化的时候,我们避免数据的查询操作直接连接数据库,而是去    内容中读取数据,这样就大大降低了数据库的读写次数,而且从内存中读数据的速度 ...

  6. 简析 __init__、__new__、__call__ 方法

    简析 __init__.__new__.__call__ 方法 任何事物都有一个从创建,被使用,再到消亡的过程,在程序语言面向对象编程模型中,对象也有相似的命运:创建.初始化.使 用.垃圾回收,不同的 ...

  7. HDU1262-寻找素数对

    //#include<bits/stdc++.h> #include<map> #include<cstdio> #include<string> #i ...

  8. github使用步骤

    首先需要注册一个github账号 1.认识github首页界面 2.如何新建一个自己的仓库 3.创建README文件 4.创建自己的文件 5.解析文件 6.生成地址 7.如何修改编辑文件

  9. 用Ubuntu快速安装Jenkins

    一.安装操作系统,安装前准备. 1.操作系统:Ubuntu 18.04 (大家都知道Ubuntu的特点,在线安装,方便很多) 2.apt源.apt源在官网上面分很多种,每个版本的源不一样,如果是其他版 ...

  10. 【UOJ117】 欧拉回路(欧拉回路)

    传送门 UOJ Solution 无解 t=1,无向图,当且仅当\(\exists i \ \ in_i \ne out_i\) t=2,有向图,当且仅当\(\exists i \ \ in_i是奇数 ...