mapState, mapActions
<template>
<div class="box">
<header class="header">分类头部</header>
<div class="content">
<div class="kind">
<div class="left">
<ul>
<li :class="kindindex === index ? 'active' : ''" v-for="(item, index) of kindlist" @click="getBrand(item, index)" :key="index">{{ item }}</li>
</ul>
</div>
<div class="right">
<div class="top">
<ul>
<li :class="brandindex === index ? 'active' : ''" v-for="(item, index) of brandlist" :key = "index" @click="getlist(item, index)">
<!-- <img :src="item.barndimg" alt=""> -->
{{ item.brand }}
</li>
</ul>
</div>
<Prolist :prolist = "prolist"/>
</div>
</div>
</div>
</div>
</template>
<script>
import { mapState, mapActions } from 'vuex'
import Prolist from '@/components/Prolist'
export default {
components: {
Prolist
},
computed: {
...mapState({
kindlist: ({ kind: { kindlist } }) => kindlist,//kind=state.kind;kindlist=state.kind.kindlist
brandlist: (state) => state.kind.brandlist,
prolist: ({ kind }) => kind.prolist //kind=state.kind
})
},
data () {
return {
kindindex: 0,
brandindex: 0
}
},
created () {
// this.$store.dispatch('getKindlist').then(data => {
// if (data === 0) {
// this.$router.push('/login')
// } else {
// this.getBrand(this.kindlist[0], 0)
// }
// })
this.getKindlist().then(data => {
if (data === 0) {
this.$router.push('/login')
} else {
this.getBrand(this.kindlist[0], 0)
}
})
},
methods: {
...mapActions({
getKindlist: 'kind/getKindlist' // 自动生成一个函数 this.$store.dispatch('getKindlist') key值为组件自定义的函数,value为状态管理器action的名字,在需要的地方触发 自定义的函数即可
}),
getBrand (item, index) {
this.kindindex = index
this.brandindex = 0
this.$store.dispatch('kind/getBrandlist', { item }).then(data => {
if (data === 0) {
this.$router.push('/login')
} else {
this.getlist(this.brandlist[0], 0)
}
})
},
getlist (item, index) {
console.log('item', item)
this.brandindex = index
this.$store.dispatch('kind/getProlist', { brand: item.brand }).then(data => {
if (data === 0) {
this.$router.push('/login')
}
})
}
}
}
</script> <style lang="scss">
@import '@/lib/reset.scss';
.content {
.kind {
@include rect(100%, 100%);
@include flexbox();
.left {
@include rect(1rem, 100%);
@include border(0 1px 0 0, #efefef, solid);
// @include background-color(#00f);
ul {
@include rect(100%, 100%);
li{
@include rect(100%, 0.36rem);
@include border(0 0 1px, #efefef, solid);
@include line-height(0.36rem);
@include text-align();
&.active {
@include color(#f66);
}
}
}
}
.right {
@include flex();
@include rect(auto, 100%);
// @include background-color(#0f0);
@include overflow();
.top {
@include rect(100%, auto);
ul {
li {
@include rect(50%, 0.3rem);
@include overflow(hidden);
@include line-height(0.3rem);
@include text-align();
@include display(inline-block);
img {
@include rect(100%, auto);
}
&.active {
@include border(1px, #f66, solid);
}
}
}
}
}
}
}
</style>
import axios from '@/utils/request'
export default {
namespaced: true,
state: {
kindlist: [],
brandlist: [],
prolist: []
},
actions: {
getKindlist (context) {
let url = '/pro/type?type=type'
return new Promise(resolve => {
axios.get(url).then(res => {
console.log('kind', res.data)
if (res.data.code === '10119') {
// this.$router.push('/login')
resolve(0)
} else {
// this.kindlist = res.data.data
// this.getBrand(this.kindlist[0], 0)
context.commit({
type: 'changeKindlist',
data: res.data.data
})
resolve(1)
}
})
})
},
getBrandlist (context, data) {
let url = '/pro/category?type=' + data.item
return new Promise(resolve => {
axios.get(url).then((res) => {
console.log(res.data)
if (res.data.code === '10119') {
resolve(0)
} else {
// this.brandlist = res.data.data
// this.getlist(this.brandlist[0], 0)
context.commit({
type: 'changeBrandlist',
data: res.data.data
})
resolve(1)
}
})
})
},
getProlist (context, data) {
let url = '/pro/brandcategory?brand=' + data.brand
return new Promise(resolve => {
axios.get(url).then((res) => {
console.log(res.data)
if (res.data.code === '10119') {
resolve(0)
} else {
context.commit({
type: 'changeProlist',
data: res.data.data
})
resolve(1)
}
})
})
}
},
mutations: {
changeKindlist (state, data) {
state.kindlist = data.data
},
changeBrandlist (state, data) {
state.brandlist = data.data
},
changeProlist (state, data) {
state.prolist = data.data
}
}
}
mapState, mapActions的更多相关文章
- 理解Vuex的辅助函数mapState, mapActions, mapMutations用法
在讲解这些属性之前,假如我们项目的目录的结构如下: ### 目录结构如下: demo1 # 工程名 | |--- dist # 打包后生成的目录文件 | |--- node_modules # 所有的 ...
- [转]理解Vuex的辅助函数mapState, mapActions, mapMutations用法
原文地址:https://www.cnblogs.com/tugenhua0707/p/9794423.html 在讲解这些属性之前,假如我们项目的目录的结构如下: ### 目录结构如下: demo1 ...
- Vuex中mapState的用法
Vuex中mapState的用法 今天使用Vuex的时候遇到一个坑,也可以说是自己的无知吧,折腾了好久,终于发现自己代码的错误了.真是天雷滚滚~~~~~~ index.js import Vue ...
- Vue状态管理vuex
前面的话 由于多个状态分散的跨越在许多组件和交互间各个角落,大型应用复杂度也经常逐渐增长.为了解决这个问题,Vue提供了vuex.本文将详细介绍Vue状态管理vuex 引入 当访问数据对象时,一个 V ...
- vue学习之vuex
1 首先还是安装 npm install vuex --save. 2 在src这种创建目录为store 创建 index.js (getters.js ,actions.js ,mutation ...
- 解决刷新页面vuex store中数据丢失的问题
**问题背景:**页面刷新后,vuex中的数据丢失.这是因为:js代码是运行在内存中的,代码运行时的所有变量.函数也都是保存在内存中的.进行刷新页面的操作,以前申请的内存被释放,重新加载脚本代码,变量 ...
- vue+vuex 回退定位到初始位置
先放出两张图(没错,你还在9012,做为一名资深设计师我唯一的技能点就是留白),简单说明下问题未做回退定位(从落地页回退,每次都回到A位置)想死啊有木有,每次都需要手动重新定位来选择,你大哥看到你做个 ...
- vuex2.0 基本使用(3) --- getter
有的组件中获取到 store 中的state, 需要对进行加工才能使用,computed 属性中就需要写操作函数,如果有多个组件中都需要进行这个操作,那么在各个组件中都写相同的函数,那就非常麻烦,这 ...
- 使用vue iview遇到的一些问题
使用阿里巴巴图标库 下载代码 这五个文件 iconfount.css 如果导入多个文件记得把@font-face复制到里面 改成./路径 //main.js import './assets/font ...
- vuex-Module
Vuex 允许将 store 分割成模块(module). 每个模块拥有自己的 state.mutation.action.getter.甚至是嵌套子模块 const moduleA = { stat ...
随机推荐
- mysql之字段约束-第五篇
数据库的完整性约束是为了在数据库应用中保证数据的一致性和正确性.所以在创建数据表时定义其字段的约束是非常有必要的. 主键约束 主键(primary key)是一个列或者列的组合,其值能唯一地标识表中的 ...
- 07 如果再使用animateCC2018或者苹果系统使用animate时出现Uncaught ReferenceError: lib is not defined的错误
我刚开始还以为是模板造成的,换了个没有问题的模板就没有问题了,原来问题是这样. 点我预览,请看该文章
- 项目自动备份,oracle 自动备份
1 项目备份 变量的形式 定时任务不执行就都写成了绝对路径 #!/bin/bash # # 项目路径 /usr/local/tomcat-bjkjdx 备份文件路径/usr/local/ba ...
- 音速启动 Vstart 5.7 win10手动移除后台设置主页
Vstart 用了快12年了,用Wireshark跟踪确实会访问广告页面,也会去上传数据.还会悄悄设置主页 ,所以在Win10上老是被干掉 也想着换 CLaunch 确实不喜欢. Rolan 买了一年 ...
- Optional类与使用==判断null有什么区别?使用Optional类有什么优势?
1.使用object==null的例子 2.null带来的问题 3.其他语言中null的处理(替代) 4.Java8的Optional类 4.1 这样做有什么好处呢? 4.2 引入Optional类的 ...
- module ‘pip‘ has no attribute ‘pep425tags‘的解决方案
可行方案: E:\pyth\Anaconda\envs>python -m pip debug --verboseWARNING: This command is only meant for ...
- Maven-高级
Mvaen 高级 1 导入其他工程 资源文件 先install到仓库里 然后根据组织名项目名版本号写在dependence里正常导入 <dependence> <groupId> ...
- SQLServer 远程链接MySql数据库
第一步:安装MySQL odbc driver 在SQL SERVER所在主机上安装MYSQL ODBC Driver; 下载地址:http://dev.mysql.com/downloads/con ...
- 在LaTeX中使用BibTeX时的一个问题及其解决:编译PDF不随bib文件更新
问题:更新bib文件之后,编译tex文件输出的PDF文件没有相应的更新. 原理: bbl文件才是引文的信息,bib文件只是用于生成bbl文件的一个"数据集"而已. 一般来说,LaT ...
- Crypto入门 (十一)easychallenge
前言: 这题跟python有关,可见看懂python代码还是很有必要得,需要有一些python基础才好 easychallenge: 题目: 下载后来发现是一个.pyc为后缀得文件,查找资料可知,该文 ...