如果你之前使用过vue.js,你一定知道在vue中各个组件之间传值的痛苦,在vue中我们可以使用vuex来保存我们需要管理的状态值,值一旦被修改,所有引用该值的地方就会自动更新,那么接下来我们就来学习一下vuex是如何修改状态值的。

1. 搭建Demo

使用vue create 项目名创建一个项目,在src根目录下创建一个components目录并在该目录下面创建2个组件:header.vue,content.vue,在App.vue根组件中进行引入然后展示,删除不必要的组件。

<template>
<div style="height:60px;" class="header">
<span></span>
<span></span>
</div>
</template> <script>
export default { }
</script> <style>
.header span{
display: inline-block;
width: 32px;
height: 32px;
cursor: pointer;
}
.header span:first-child{
background: red;
} .header span:last-child {
background: blue;
} </style>

header.vue

<template>
<div class="content" :style="`background: pink`">
Content
</div>
</template> <script>
export default {
mounted(){ }
}
</script> <style>
.content {
height: 600px;
text-align: center;
line-height: 600px;
}
</style>

content.vue

<template>
<div id="app">
<Header></Header>
<Content></Content>
</div>
</template> <script>
import Header from "./components/header";
import Content from "./components/content";
export default {
// 注册组件
components: {
Header,
Content
}
};
</script> <style>
</style>

App.vue

页面效果:

下面需要实现点击红色按钮使得背景变为红色,点击蓝色按钮使得背景变成蓝色,下面使用vuex实现。

2. 设置和获取仓库中的数据

先安装vuex。

yarn add vuex

在src根目录下创建store,然后创建一个index.js文件。

import Vuex from "vuex";
import Vue from "vue"; // 注册插件
Vue.use(Vuex); // 创建仓库实例
const store = new Vuex.Store({
// 仓库的数据
state: {
test_data: "this is some test data",
color: "light-green"
}, // 同步修改state的值
mutations: {
// mutations下的方法第一个参数是固定state
// 第二个参数是传进来的参数
setColor(state, color) {
state.color = color;
}
}
}); export default store;

stroe/index.js

实现后的代码

<template>
<div style="height:60px;" class="header">
<span @click="handleClick('red')"></span>
<span @click="handleClick('blue')"></span>
</div>
</template> <script>
export default {
methods: {
handleClick(color){
// 不推荐使用这个方法来修改state的值
// this.$store.state.color = color; // this.$store.commit调用mutations下面的方法
// 第一个参数就是mutations下面的的具体方法
// 第二个参数就是传递给方法的参数
this.$store.commit("setColor", color)
}
}
}
</script> <style>
.header span{
display: inline-block;
width: 32px;
height: 32px;
cursor: pointer;
}
.header span:first-child{
background: red;
} .header span:last-child {
background: blue;
} </style>

header.vue

<template>
<div class="content" :style="`background: ${$store.state.color}`">Content</div>
</template> <script>
export default {
mounted() {
// this.$store当把仓库引入到main.js的时候,组件可以通过this.$stroe获取仓库的数据
console.log(this.$store);
}
};
</script> <style>
.content {
height: 600px;
text-align: center;
line-height: 600px;
}
</style>

content.vue

import Vue from 'vue'
import App from './App.vue'
import store from './store' Vue.config.productionTip = false new Vue({
render: h => h(App),
store,
}).$mount('#app')

main.js

vuex之仓库数据的设置与获取的更多相关文章

  1. 072——VUE中vuex之使用mutations修改购物车仓库数据

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. vuex数据管理-数据模块化

    对于vue这类mvvm框架来说,其核心就是组件与数据,因此做好相应的数据管理极为重要.这里分享下vuex数据模块化管理的方法,有利于搭建便于维护.协作的vue项目. vuex管理基本方法和使用 模块化 ...

  3. vuex数据管理-数据适配

    由于接口在上线前,不可避免的会出现变动,小则属性名变,大则结构变化.如果处理不当,结构变化时,视图的代码也需要做相应的更改,然后就是容错方法的变动,接着重新自测等,这样,变化成本随着结构的复杂度大大加 ...

  4. 封装cookie设置和获取的简易方法

    (function() { var tool = { expires: "expires", // 过期时间expires path: "path", // 路 ...

  5. django设置并获取cookie/session,文件上传,ajax接收文件,post/get请求及跨域请求等的方法

    django设置并获取cookie/session,文件上传,ajax接收文件等的方法: views.py文件: from django.shortcuts import render,HttpRes ...

  6. 爬虫 Http请求,urllib2获取数据,第三方库requests获取数据,BeautifulSoup处理数据,使用Chrome浏览器开发者工具显示检查网页源代码,json模块的dumps,loads,dump,load方法介绍

    爬虫 Http请求,urllib2获取数据,第三方库requests获取数据,BeautifulSoup处理数据,使用Chrome浏览器开发者工具显示检查网页源代码,json模块的dumps,load ...

  7. UWP入门——应用数据和设置

    数据有两个基本的分类,应用数据和用户数据,而用户数据则为由用户拥有的数据,如文档,音乐或电子邮件等,下面将大致的介绍一下应用数据的基本操作. 应用数据:应用数据包含APP的状态信息(如运行时状态,用户 ...

  8. 2017.9.27 JavaWeb 属性的设置和获取

    3.4.3新属性的设置和获取 对于getpParamter方法是通过参数传递获得数据, 设置数据的方法格式: void  request.setAttribute("key",Ob ...

  9. Net Core 中WebAPI有关 Session的设置,及获取

    步骤一: 在Startup 文件中做相应的设置 ConfigureServices方法里添加 //ConfigureServices添加: services.AddSession(options =& ...

随机推荐

  1. 【vue】imitate-beautiful-thing

    我从未见过这么美妙的项目,当然与我接触的项目少有关,但是这个项目满满的艺术气息,让人沉醉,让人忍不住的去研究代码. 先放项目地址:https://github.com/eidonlon/imitate ...

  2. bert 预训练模型路径

    google的bert预训练模型: BERT-Large, Uncased (Whole Word Masking): 24-layer, 1024-hidden, 16-heads, 340M pa ...

  3. [转]js作用域系列——内部原理

    前面的话 javascript拥有一套设计良好的规则来存储变量,并且之后可以方便地找到这些变量,这套规则被称为作用域.作用域貌似简单,实则复杂,由于作用域与this机制非常容易混淆,使得理解作用域的原 ...

  4. idea中HTML格式化时标签缩进问题

    在IntelliJ Idea中HTML格式化时,默认<head><body>以及<body>下的以及标签都不会缩进. 解决方法:editor->code st ...

  5. 删除 BIRT Report Viewer

    去掉首页上的标题BIRT Report Viewer方法:找到Webroot\webcontent\birt\pages\layout\FramesetFragment.jsp文件,在里面定义了标题, ...

  6. 关于java中的异常

    java中有时候要写形如下图中的方法抛出异常 之所以要这么写(要在方法声明行写上throws ...)是因为这种 FileNotFoundException 属于编译异常 不属于运行时异常 不会主动抛 ...

  7. 13类100个常用Linux基础命令

    玩过Linux的人都会知道,Linux中的命令的确是非常多,但是玩过Linux的人也从来不会因为Linux的命令如此之多而烦恼,因为我们只需要掌握我们最常用的命令就可以了.然而每个人玩Linux的目的 ...

  8. centos 安装redis2.8.9

    1没有安装gcc yum install gcc-c++ 2. 安装tcl yum install -y tcl 3.安装redis $ wget http://download.redis.io/r ...

  9. day37 04-Hibernate二级缓存:list和iterate方法比较

    get()和load()方法既可以向一级缓存区放数据,也可以向二级缓存区放数据.这是查询一个的情况.要是查询所有呢?注意, // 查询所有.Query接口的list()方法. // list()方法会 ...

  10. Python 数据文件操作——写出数据