main.js
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import {store} from './vuex/store'
Vue.config.productionTip = false
/* eslint-disable no-new */
new Vue({
store:store,
el: '#app',
router,
components: { App },
template: '<App/>'
})
store.js
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export const store = new Vuex.Store({
strict:true, //严格模式
//数据 (存数据)
state:{
products:[
{name:"马云",price:200},
{name:"马化腾",price:140},
{name:"马冬梅",price:20},
{name:"马蓉",price:10}
]
},
//数据方法(获取数据)
getters:{
//获取数据state进行操作
saleProducts: (state) =>{
var saleProducts = state.products.map(product =>{
return {
name: "**" + product.name + "**",
price: product.price / 2
};
});
return saleProducts;
}
},
//触发事件改变数据 (同步.如果异步的话,改变不了)
mutations:{
reducePrice: (state,payload) =>{
// setTimeout(function(){
state.products.forEach(product =>{
product.price -= payload;
})
// },3000);
}
},
//触发事件改变数据 (异步)
actions:{
//context 类似于 this.$store.
reducePrice:(context,payload) =>{
setTimeout(function(){
context.commit("reducePrice",payload);
},2000);
}
}
});
A.vue
<template>
<div id="product-list-one">
<h2>Product List One</h2>
<ul>
<li v-for="product in saleProducts">
<span class="name">{{product.name}}</span>
<span class="price">${{product.price}}</span>
</li>
</ul>
<button @click="reducePrice(4)">商品降价</button>
</div>
</template>
<script>
import {mapGetters} from 'vuex'
import {mapActions} from 'vuex'
export default {
//拿vuex参数
computed:{
pruducts(){
return this.$store.state.products
},
//调用vuex里面的方法
saleProducts(){
return this.$store.getters.saleProducts;
}
},
methods:{
reducePrice(amount){
// this.$store.commit('reducePrice');
this.$store.dispatch("reducePrice",amount);
}
}
}
</script>
<style scoped>
#product-list-one{
background: #FFF8B1;
box-shadow: 1px 2px 3px rgba(0,0,0,0.2);
margin-bottom: 30px;
padding: 10px 20px;
}
#product-list-one ul{
padding: 0;
}
#product-list-one li{
display: inline-block;
margin-right: 10px;
margin-top: 10px;
padding: 20px;
background: rgba(255,255,255,0.7);
}
.price{
font-weight: bold;
color: #E8800C;
}
</style>
B.js
<template>
<div id="product-list-two">
<h2>Product List Two</h2>
<ul>
<li v-for="product in saleProducts">
<span class="name">{{product.name}}</span>
<span class="price">${{product.price}}</span>
</li>
</ul>
<!-- <button @click='reducePrice'>商品降价</button> -->
</div>
</template>
<script>
export default {
computed:{
Products(){
return this.$store.state.products
},
saleProducts(){
return this.$store.getters.saleProducts;
}
}
}
</script>
<style scoped>
#product-list-two{
background: #D1E4FF;
box-shadow: 1px 2px 3px rgba(0,0,0,0.2);
margin-bottom: 30px;
padding: 10px 20px;
}
#product-list-two ul{
padding: 0;
list-style-type: none;
}
#product-list-two li{
margin-right: 10px;
margin-top: 10px;
padding: 20px;
background: rgba(255,255,255,0.7);
}
.price{
font-weight: bold;
color: #860CE8;
display: block;
}
</style>