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. CentOS 7 rabbitmq 安装

    OS版本:CentOS 7.2Rrlang:19.2RabbitMQ:3.6.6 1.1 erlang安装 1. http://www.erlang.org/下载erlang,解压缩,进入目录,检查环 ...

  2. Spring的声明式事务管理<tx:advice/>

    <tx:advice/> 有关的设置 这一节里将描述通过 <tx:advice/> 标签来指定不同的事务性设置.默认的 <tx:advice/> 设置如下: 事务传 ...

  3. shell编写自动化安装dhcp服务

    #!/bin/bash#Auth:Darius#自动化安装dhcp服务#"$1"为测试IP,用来查看IP段是否能通eno=`ifconfig|awk '{print $1}'|he ...

  4. mvc开发网站打开慢总结

    开始学习mvc开发网站的时候,看了传智博客的视频教程,其中学习了一个和牛逼的框架,开始激动的深入学习,学完后却发现其实那套框架太重并不适合一些中小型的网站开发,并且也使用导航属性关联外键,导致打开网站 ...

  5. 安装PyQt5时缺少designer.exe的解决办法

    学习PyQt框架的时候,看到了可以用可视化的方法搭建界面,好像ios的xib,但是安装完成pyqt5后怎么都找不designer.exe这个文件,于是查到了一下.发现了可以通过安装pip instal ...

  6. Linux-系统编程-知识点概述

    1.基本指令和5个背景知识(os.env.file.shell.权限) 2.开发环境:(vim.gcc.g++.gdb.ctags.make.Makefile.procbar) 3.进程1: 进程的基 ...

  7. 谈谈 TCP 的 TIME_WAIT

    由来 最近有同事在用 ab 进行服务压测,到 QPS 瓶颈后怀疑是起压机的问题,来跟我借测试机,于是我就趁机分析了一波起压机可能成为压测瓶颈的可能,除了网络 I/O.机器性能外,还考虑到了网络协议的问 ...

  8. Codeforces Round #554 (Div. 2) 1152B. Neko Performs Cat Furrier Transform

    学了这么久,来打一次CF看看自己学的怎么样吧 too young too simple 1152B. Neko Performs Cat Furrier Transform 题目链接:"ht ...

  9. flask上下文(new)

    flask源码解析之上下文 引入 对于flask而言,其请求过程与django有着截然不同的流程.在django中是将请求一步步封装最终传入视图函数的参数中,但是在flask中,视图函数中并没有请求参 ...

  10. IntelliJ IDEA 的下载和安装

    下载 官网地址:https://www.jetbrains.com/idea/ 直接点击 DOWNLOAD 下载 接下来跳转到一个页面,可以看到第一个红框中是选择操作系统的,IDEA分为收费的旗舰版和 ...