如果你之前使用过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. cmd命令调用powershell脚本方法

    cmd方法: powershell -command ". ('ps1脚本路径'); WriteInfo  -param 'param参数值'" ps1脚本代码: function ...

  2. Javascript-简单的计时钟表

    <!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...

  3. TZ_16_Vue的idea入门

    1.Vue (读音 /vjuː/,类似于 view) 是一套用于构建用户界面的渐进式框架.与其它大型框架不同的是,Vue 被设计为可以自底向上逐层应用.Vue 的核心库只关注视图层,不仅易于上手,还便 ...

  4. hdu oj 1520 Anniversary party(树形dp入门)

    Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  5. spring深入学习(六)-----springmvc

    MVC设计模式 有过一定开发经验的人肯定都知道这个模式,先简单介绍下这种模式,然后再去讨论为啥要这么设计: 传统的web应用中应该主要包括这些组件,不同组件负责不同的模块. 数据实体:POJO 数据层 ...

  6. 推荐大家自学的java学习网站,生动的讲解适合刚入门

    java学习网站(不仅仅是只学习java的知识):http://how2j.cn 首先大家来看看这个网站都有些啥 首页:图中的左侧目录大家看到了,从java基础到高级,从后台技术到前端页面,数据库,还 ...

  7. 外观模式(Facade)(门面模式、子系统容易使用)

    外观(Facade)模式的定义:是一种通过为多个复杂的子系统提供一个一致的接口,而使这些子系统更加容易被访问的模式.该模式对外有一个统一接口,外部应用程序不用关心内部子系统的具体的细节,这样会大大降低 ...

  8. Leetcode49. Group Anagrams字母异位词分组

    给定一个字符串数组,将字母异位词组合在一起.字母异位词指字母相同,但排列不同的字符串. 示例: 输入: ["eat", "tea", "tan&quo ...

  9. Leetcode48. Rotate Image旋转图像

    给定一个 n × n 的二维矩阵表示一个图像. 将图像顺时针旋转 90 度. 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵.请不要使用另一个矩阵来旋转图像. 示例 1: 给定 m ...

  10. WordPress不同分类使用不同的文章模板

    倡萌昨天分享的 Custom Post Template 和 Single Post Template 可以让你自定义每篇文章的文章模板,今天来说说WordPress不同分类使用不同的文章模板. 方法 ...