vuex之仓库数据的设置与获取
如果你之前使用过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之仓库数据的设置与获取的更多相关文章
- 072——VUE中vuex之使用mutations修改购物车仓库数据
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- vuex数据管理-数据模块化
对于vue这类mvvm框架来说,其核心就是组件与数据,因此做好相应的数据管理极为重要.这里分享下vuex数据模块化管理的方法,有利于搭建便于维护.协作的vue项目. vuex管理基本方法和使用 模块化 ...
- vuex数据管理-数据适配
由于接口在上线前,不可避免的会出现变动,小则属性名变,大则结构变化.如果处理不当,结构变化时,视图的代码也需要做相应的更改,然后就是容错方法的变动,接着重新自测等,这样,变化成本随着结构的复杂度大大加 ...
- 封装cookie设置和获取的简易方法
(function() { var tool = { expires: "expires", // 过期时间expires path: "path", // 路 ...
- django设置并获取cookie/session,文件上传,ajax接收文件,post/get请求及跨域请求等的方法
django设置并获取cookie/session,文件上传,ajax接收文件等的方法: views.py文件: from django.shortcuts import render,HttpRes ...
- 爬虫 Http请求,urllib2获取数据,第三方库requests获取数据,BeautifulSoup处理数据,使用Chrome浏览器开发者工具显示检查网页源代码,json模块的dumps,loads,dump,load方法介绍
爬虫 Http请求,urllib2获取数据,第三方库requests获取数据,BeautifulSoup处理数据,使用Chrome浏览器开发者工具显示检查网页源代码,json模块的dumps,load ...
- UWP入门——应用数据和设置
数据有两个基本的分类,应用数据和用户数据,而用户数据则为由用户拥有的数据,如文档,音乐或电子邮件等,下面将大致的介绍一下应用数据的基本操作. 应用数据:应用数据包含APP的状态信息(如运行时状态,用户 ...
- 2017.9.27 JavaWeb 属性的设置和获取
3.4.3新属性的设置和获取 对于getpParamter方法是通过参数传递获得数据, 设置数据的方法格式: void request.setAttribute("key",Ob ...
- Net Core 中WebAPI有关 Session的设置,及获取
步骤一: 在Startup 文件中做相应的设置 ConfigureServices方法里添加 //ConfigureServices添加: services.AddSession(options =& ...
随机推荐
- UVA11916 Emoogle Grid
Emoogle Grid You have to color an M × N (1 ≤ M, N ≤ 108 ) two dimensional grid. You will be provided ...
- Go之路一
一.声明变量 var a int var b string var c []float32 var d func() bool var e struct{ x int } 第1行,声明一个整型类型的变 ...
- 如何处理HTML5新标签的浏览器兼容问题?
方法一 : 1.使用静态资源的html5shiv包 <!--[if lt IE9]> <script src="http://cdn.static.runoob.com/l ...
- Liferay 7:Liferay DXP解决方案
分享是美德,欢迎探讨技术 这个作者很厉害呀,写的博客都是解决很刁钻问题的.强烈推荐 http://www.liferaysolution.com/2017/06/captcha-recaptcha-w ...
- CI框架 - Xhprof性能监控,用钩子hooks实现
安装Xhprof参考:http://www.cnblogs.com/qq917937712/p/8889001.html 第一步:配置config.php $config['enable_hooks' ...
- bootstrap-datetimepicker下ie8对indexOf的支持问题
问题: 由于ie8不支持indexOf这个方法,所以在引入bootstrap-datetimepicker.js的时候js会抛出错误. 解决: // 在bootstrap-datetimepicker ...
- 前端(Node.js)(2)-- Node.js开发环境配置
1.开发环境介绍 1.MEAN Stack 什么是全栈? 负责界面和UI的设计师.负责移动端应用开发的安卓IOS开发工程师.负责服务器端开发的后端程序员.负责数据库开发和管理的数据库工程师.负责服务器 ...
- PHP实现微信申请退款流程实例源码
https://www.jb51.net/article/136476.htm 目录 前期准备: 前面讲了怎么实现微信支付,详见博文:PHP实现微信支付(jsapi支付)流程 和ThinkPHP中实 ...
- JDBC 操作数据库实例
JDBC是什么 JDBC代表Java数据库连接(Java Database Connectivity),它是用于Java编程语言和数据库之间的数据库无关连接的标准Java API,换句话说:JDBC是 ...
- 关于python的元组操作
关于元组: 元组和列表是类似的,但是元组中的数据是不可以修改的. 元组是一对 () 元组操作: 元组是不可以修改的所以对元组的操作极少 定义空元组(因为元组一旦创建,数据不可被修改,所以极少创建空元组 ...