如果你之前使用过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. 2019-8-31-dotnet-通过-WMI-获取指定进程的输入命令行

    title author date CreateTime categories dotnet 通过 WMI 获取指定进程的输入命令行 lindexi 2019-08-31 16:55:59 +0800 ...

  2. html 输入框显示“小叉叉”的清空方法

    在IE10以下,我们的输入框input会出现小叉叉.怎么解决这个问题呢? 针对input框我们做一个处理 <style type="text/css"> input:: ...

  3. Puppet基础应用

    Puppet简介 IT基础设施自动化管理工具,作者:Luck Kanies,官方站点:www.puppetlabs.com 管理设施的整个生命周期: provisioning.configuratio ...

  4. js校验文本框只能输入数字(包括小数)

    form表单 <form method="POST" action=""> <input type="text" id=& ...

  5. stream之累加求和

    1.集合中直接包含BigDecimal元素的累加 List<Integer> list = new ArrayList<>();list.add(3);list.add(7); ...

  6. Browsersync 浏览器自动刷新

    Browsersync 是一个很好用的工具,它可以实时监测文件的变动然后自动刷新浏览器,不用每次去点刷新或F5,特别在调试样式时非常有用. browsersync中文网  http://www.bro ...

  7. C++ std::map用法简介

    #include "map" //引入头文件 初始化: std::map <int, std::string> _map1; //初始化 //c++11中引入的,可以直 ...

  8. html中有序列表标签ol,li的高级应用

    本文主要介绍html中有序列表标签ol,li的高级应用, 在网页设计时我们设计有序列表内容时,经常会在每个ITEM前手工加上一个数值,或是由程序加上这个数值. 而如果使用有序列表标签ol和li,则不需 ...

  9. jQuery中的工具和插件

    jQuery的工具属性 jQuery类数组操作 length属性 表示获取类数组中元素的个数 get()方法 表示获取类数组中单个元素"括号中填写该元素的索引值" index()方 ...

  10. java 操作CLOB类型数据

    clob类型,但对于这个类型处理起来还是比较麻烦的,varchar2长度为4000bytes,如果varchar2能满足楼主的需求,建议使用varchar2,下面提供了在Java 中读取clob类型的 ...