使用vuex的流程随笔
1.在建好的vue项目中新建一个vuex文件夹在此文件夹下建一个index.js文件,在此文件下引入vuex 模块(当然需要先npm下载)和vue模块,在引入你所有的自定义的module.js模块(下边会讲module的内容),然后使用vuex
Vue.use(Vuex) ;在导出时新创建一个Vuex的Store的对象,在这个对象中使用modules:{把你自定义的模块赋值}代码如下:
import Vuex from 'vuex'
import Vue from 'vue'
import home from '../page/home/moudlehome.js';
import city from '../page/city/moudlecity.js';
Vue.use(Vuex);
export default new Vuex.Store({
modules:{
home:home, //this.$store.state.home
city:city //this.$store.state.city
}
})
2.在module.js中使用vuex的核心内容并发送axios请求(前提是在页面渲染完后就发送请求所以需要在总组件中设置mounted() { if(this.$store.getters.shouldGetData){
this.$store.dispatch("getSwiperInfo");
} }来传值给module中的actions并且在getSwiperInfo()函数中发送axios异步请求,代码如下:
import axios from 'axios';
export default {
//数据内容
state:{
swiperInfo: [],
swiperInfo1: []
},
//异步操作
actions:{
getSwiperInfo(context){
axios.get('/static/index.json')
.then((response)=>{
if (response.status === 200) {
const {data} = response.data;
context.commit("changeSwiperInfo",data)
}
})
}
},
//对数据内容的更新
mutations:{
changeSwiperInfo:(state,data)=>{
state.swiperInfo = data.swiperInfo;
state.swiperInfo1 = data.swiperInfo1
}
},
//对数据的二次包装对网页的一种优化
getters:{
shouldGetData(state){
if(!state.swiperInfo.length&&
!state.swiperInfo1.length){
return true;
}else{
return false;
}
}
}
}
3.在子组件中绑定并使用数据、
在子组件中使用
computed:{
swiperInfo(){
return this.$store.state.home.swiperInfo;
}
}
绑定数据
然后就可以在你的实例化循环中使用swiperInfo in item来使用数据了
使用vuex的流程随笔的更多相关文章
- vuex介绍和vuex数据传输流程
1.什么是vuex? 公共状态管理:解决多个非父子组件传值麻烦的问题:简单说就是多个页面都能用Vuex中store公共的数据 a.并不是所有的数据都要放在Vuex中,只有各个组件公用的一些数据会放在V ...
- Vue--- 使用vuex使用流程 1.0
Vuex 1.安装vuex npm install -save vuex 2. 引入 创建store文件夹目录 创建 vuex 指挥公共目录 store['state','action ...
- TT流程随笔
细节: 如果本地可以自动登录, 先实现本地登录,发送事件通知,再请求登录服务器 如果本地不可以登录(第一次或退出后),直接请求登录服务器 登录服务器返回消息服务器ip port / 文件服务器 链接消 ...
- vuex:使用思路总结
1. vuex是什么? 是一种数据状态管理机制. 2.vuex的构成和作用: state: 存放需要被管理的属性的对象 getters: 方便在state中做集中处理,可以把state作为第一个参数 ...
- 说一说Vuex有哪几种状态和属性
vuex的流程 页面通过mapAction异步提交事件到action.action通过commit把对应参数同步提交到mutation mutation会修改state中对应的值.最后通过getter ...
- vue2.0自学笔记
前言: 一.优点: 轻量级.高效率.上手快.简单易学.文档全面而简洁 二.功能: 1.模板渲染 2.模块化 3.扩展功能:路由.Ajax 三.课程包含: 1.Vue实例 2.Vue组件 3.Vue指令 ...
- vue 工作学习总结
配置ESlint yarn 初始化 yarn init yes 添加依赖 yarn add [package] 升级依赖 yarn upgrade [package] 移出依赖 yarn remove ...
- </2017><2018>
>>> Blog 随笔原始文档及源代码 -> github: https://github.com/StackLike/Python_Note >>> 统计信 ...
- React与Vue的相同与不同点
我们知道JavaScript是世界上最流行的语言之一,React和Vue是JS最流行的两个框架.所以要想前端的开发那么必须掌握好这两个框架. 那么这两个框架有什么不同呢? React 和 Vue 相同 ...
随机推荐
- 注解实战@Test标签
1.创建一个Maven工程 2.点击java,右键-新建一个类 package com.course.testng; import org.testng.annotations.Test; publi ...
- SOAP扩展PHP轻松实现WebService
最近在一个PHP项目中对接外部接口涉及到WebService,搜索引擎上相关文章不是很多,找到的大都是引用一个号称很强大的开源软件 NuSOAP(下载地址:http://sourceforge.net ...
- LeetCode 11. Container With Most Water 单调队列
题意 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai ...
- centos7下部署Redis
1.1. Redis的安装 Redis是c语言开发的. 安装redis需要c语言的编译环境.如果没有gcc需要在线安装.yum install gcc-c++ 安装步骤: 第一步:redis的源码包上 ...
- (转)Epoll模型详解
1. 内核中提高I/O性能的新方法epoll epoll是什么?按照man手册的说法:是为处理大批量句柄而作了改进的poll.要使用epoll只需要这三个系统调 用:epoll_create(2), ...
- 洛谷 P2393 yyy loves Maths II
P2393 yyy loves Maths II 题目背景 上次蒟蒻redbag可把yyy气坏了,yyy说他只是小学生,蒟蒻redbag这次不坑他了. 题目描述 redbag给了yyy很多个数,要yy ...
- shell脚本学习之ubuntu删除多余内核
#!/bin/bash #定期删除内核 #存储命令输出cmd_output=`commands` uname_output=$(uname -r) kernel_output=`dpkg --list ...
- javaScript中的事件对象event
事件对象event,每当一个事件被触发的时候,就会随之产恒一个事件对象event,该对象中主要包括了关于该事件的基本属性,事件类型type(click.dbclick等值).目标元素target(我的 ...
- Codeforce 163 A. Substring and Subsequence DP
A. Substring and Subsequence One day Polycarpus got hold of two non-empty strings s and t, consist ...
- awk双文件互相匹配查找
awk双文件互相匹配查找 要求: 文件a: 10/05766798607,11/20050325191329,29/0.1,14/05766798607 10/05767158557,11/200 ...