企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] 注意下面都需要引入vuex.js #### 1.vuex之vuex的使用场景分析与state购物车实例讲解 注意需要引入vuex.js <div id="hdcms"> <lists></lists> </div> <script type="text/x-template" id="Lists"> <div> <table border="1" width="90%"> <tr><th>编号</th><th>名称</th><th>价格</th></tr> <tr v-for="v in goods"> <td>{{v.id}}</td> <td>{{v.title}}</td> <td>{{v.price}}</td> </tr> </table> <h1>总价: {{totalPrice}}</h1> </div> </script> <script> let Lists = { template: '#Lists', computed: { totalPrice(){ return this.$store.state.totalPrice; }, goods(){ return this.$store.state.goods; } } } let store = new Vuex.Store({ state: { totalPrice: 100, goods: [ {id: 1, title: 'iphone7Plus', price: 399}, {id: 2, title: 'hdcms系统', price: 1999}, ] } }); var app = new Vue({ el: '#hdcms', store, components: { Lists } }) </script> #### 2.vuex之使用getters高效的获取购物车商品总价 <div id="hdcms"> <lists></lists> </div> <script type="text/x-template" id="Lists"> <div> <h1>购物车</h1> <table border="1" width="90%"> <tr> <th>编号</th> <th>名称</th> <th>价格</th> <th>数量</th> <th>总计</th> </tr> <tr v-for="v in goods"> <td>{{v.id}}</td> <td>{{v.title}}</td> <td>{{v.price}}</td> <td>{{v.num}}</td> <td></td> </tr> </table> <h1>总价: {{totalPrice}}</h1> </div> </script> <script> let Lists = { template: '#Lists', computed: { totalPrice(){ return this.$store.getters.totalPrice; }, goods(){ return this.$store.state.goods; } } } let store = new Vuex.Store({ state: { goods: [ {id: 1, title: 'iphone7Plus', price: 399, num: 3}, {id: 2, title: 'hdcms系统', price: 1999, num: 6}, ] }, getters: { //获取商品总价 totalPrice: state => { let totalPrice = 0; state.goods.forEach((v) => { totalPrice += v.num * v.price; }) return totalPrice; } } }); var app = new Vue({ el: '#hdcms', store, components: { Lists } }) </script> #### 3.vuex之使用getters计算每一件购物车中商品的总价 <div id="hdcms"> <lists></lists> </div> <script type="text/x-template" id="Lists"> <div> <h1>购物车</h1> <table border="1" width="90%"> <tr> <th>编号</th> <th>名称</th> <th>价格</th> <th>数量</th> <th>总计</th> </tr> <tr v-for="v in goods"> <td>{{v.id}}</td> <td>{{v.title}}</td> <td>{{v.price}}</td> <td>{{v.num}}</td> <td>{{v.totalPrice}}</td> </tr> </table> <h1>总价: {{totalPrice}}</h1> </div> </script> <script> let Lists = { template: '#Lists', computed: { totalPrice(){ return this.$store.getters.totalPrice; }, goods(){ return this.$store.getters.goods; } } } let store = new Vuex.Store({ state: { goods: [ {id: 1, title: 'iphone7Plus', price: 300, num: 3}, {id: 2, title: 'hdcms系统', price: 1999, num: 6}, ] }, getters: { //获取商品总价 totalPrice: state => { let totalPrice = 0; state.goods.forEach((v) => { totalPrice += v.num * v.price; }) return totalPrice; }, //获取商品并计算每件商品的总价 goods(state){ let goods = state.goods; goods.forEach((v)=>{ v.totalPrice = v.num*v.price; }); return goods; } } }); var app = new Vue({ el: '#hdcms', store, components: { Lists } }) </script> #### 4.vuex之将购物车底部生成新组件用于统计总价 <div id="hdcms"> <lists></lists> <footer-cart></footer-cart> </div> <script type="text/x-template" id="Lists"> <div> <h1>购物车</h1> <table border="1" width="90%"> <tr> <th>编号</th> <th>名称</th> <th>价格</th> <th>数量</th> <th>总计</th> </tr> <tr v-for="v in goods"> <td>{{v.id}}</td> <td>{{v.title}}</td> <td>{{v.price}}</td> <td> <input type="text" v-model="v.num"> </td> <td>{{v.totalPrice}}</td> </tr> </table> </div> </script> <script type="text/x-template" id="footerCart"> <div class="footerCart"> 总计: {{totalPrice}} </div> </script> <style> .footerCart{background: #999;color:#fff;} </style> <script> let Lists = { template: '#Lists', computed: { goods(){ return this.$store.getters.goods; } } } let footerCart={ template:'#footerCart', computed:{ totalPrice(){ return this.$store.getters.totalPrice; } } } let store = new Vuex.Store({ state: { goods: [ {id: 1, title: 'iphone7Plus', price: 300, num: 3}, {id: 2, title: 'hdcms系统', price: 1999, num: 6}, ] }, getters: { //获取商品总价 totalPrice: state => { let totalPrice = 0; state.goods.forEach((v) => { totalPrice += v.num * v.price; }) return totalPrice; }, //获取商品并计算每件商品的总价 goods(state){ let goods = state.goods; goods.forEach((v)=>{ v.totalPrice = v.num*v.price; }); return goods; } } }); var app = new Vue({ el: '#hdcms', store, components: { Lists,footerCart } }) </script> #### 5.vuex之使用mutations修改购物车仓库数据 <div id="hdcms"> <lists></lists> <footer-cart></footer-cart> </div> <script type="text/x-template" id="Lists"> <div> <h1 v-if="goods.length==0">购物车中没有商品,<a href="http://www.baidu.com">去购物吧...</a></h1> <div v-if="goods.length>0"> <h1>购物车</h1> <table border="1" width="90%"> <tr> <th>编号</th> <th>名称</th> <th>价格</th> <th>数量</th> <th>总计</th> <th>操作</th> </tr> <tr v-for="v in goods"> <td>{{v.id}}</td> <td>{{v.title}}</td> <td>{{v.price}}</td> <td> <input type="text" v-model="v.num"> </td> <td>{{v.totalPrice}}</td> <td> <button @click="del(v.id)">删除</button> </td> </tr> </table> </div> </div> </script> <script type="text/x-template" id="footerCart"> <div class="footerCart"> <div v-if="totalPrice>0"> 总计: {{totalPrice}} </div> </div> </script> <style> .footerCart{background: #999;color:#fff;} </style> <script> let Lists = { template: '#Lists', computed: { goods(){ return this.$store.getters.goods; } }, methods:{ del(id){ this.$store.commit('del',{id}) } } } let footerCart={ template:'#footerCart', computed:{ totalPrice(){ return this.$store.getters.totalPrice; } } } let store = new Vuex.Store({ state: { goods: [ {id: 1, title: 'iphone7Plus', price: 300, num: 3}, {id: 2, title: 'hdcms系统', price: 1999, num: 6}, ] }, getters: { //获取商品总价 totalPrice: state => { let totalPrice = 0; state.goods.forEach((v) => { totalPrice += v.num * v.price; }) return totalPrice; }, //获取商品并计算每件商品的总价 goods(state){ let goods = state.goods; goods.forEach((v)=>{ v.totalPrice = v.num*v.price; }); return goods; } }, mutations:{ //删除购物车中的商品 del(state,param){ let k; for(let i=0;i<state.goods.length;i++){ if(state.goods[i].id==param.id){ k=i;break; } } state.goods.splice(k,1); } } }); var app = new Vue({ el: '#hdcms', store, components: { Lists,footerCart } }) </script> #### 6.vuex之使用actions与axios异步初始购物车数据 需要引入vuex.js 和axios.js <div id="hdcms"> <lists></lists> <footer-cart></footer-cart> </div> <script type="text/x-template" id="Lists"> <div> <h1 v-if="goods.length==0">购物车中没有商品,<a href="http://www.baidu.com">去购物吧...</a></h1> <div v-if="goods.length>0"> <h1>购物车</h1> <table border="1" width="90%"> <tr> <th>编号</th> <th>名称</th> <th>价格</th> <th>数量</th> <th>总计</th> <th>操作</th> </tr> <tr v-for="v in goods"> <td>{{v.id}}</td> <td>{{v.title}}</td> <td>{{v.price}}</td> <td> <input type="text" v-model="v.num"> </td> <td>{{v.totalPrice}}</td> <td> <button @click="del(v.id)">删除</button> </td> </tr> </table> </div> </div> </script> <script type="text/x-template" id="footerCart"> <div class="footerCart"> <div v-if="totalPrice>0"> 总计: {{totalPrice}} </div> </div> </script> <style> .footerCart { background: #999; color: #fff; } </style> <script> let Lists = { template: '#Lists', computed: { goods(){ return this.$store.getters.goods; } }, methods: { del(id){ this.$store.commit('del', {id}) } } } let footerCart = { template: '#footerCart', computed: { totalPrice(){ return this.$store.getters.totalPrice; } } } let store = new Vuex.Store({ state: { goods: [] }, getters: { //获取商品总价 totalPrice: state => { let totalPrice = 0; state.goods.forEach((v) => { totalPrice += v.num * v.price; }) return totalPrice; }, //获取商品并计算每件商品的总价 goods(state){ let goods = state.goods; goods.forEach((v) => { v.totalPrice = v.num * v.price; }); return goods; } }, mutations: { //删除购物车中的商品 del(state, param){ let k; for (let i = 0; i < state.goods.length; i++) { if (state.goods[i].id == param.id) { k = i; break; } } state.goods.splice(k, 1); }, setGoods(state,param){ state.goods=param.goods; } }, actions: { loadGoods(store){ axios.get('73.php').then(function (response) { store.commit('setGoods',{goods:response.data}) console.log(response); }); } } }); var app = new Vue({ el: '#hdcms', store, components: { Lists, footerCart }, mounted(){ this.$store.dispatch('loadGoods'); } }) </script> php代码: <?php $data = [ ['id' => 1, 'title' => 'iphone7Plus', 'price' => 300, 'num' => 3], ['id' => 2, 'title' => 'hdcms系统', 'price' => 1999, 'num' => 9], ]; echo json_encode($data); #### 7.vuex之模块化modules开发实例讲解 需要引入vuex.js 和axios.js 当使用命名空间的时候,模块里面的getters,mutations,actions,就不是全局了。 <div id="hdcms"> <lists></lists> <footer-cart></footer-cart> </div> <script type="text/x-template" id="Lists"> <div> <h1 v-if="goods.length==0">购物车中没有商品,<a href="http://www.baidu.com">去购物吧...</a></h1> <div v-if="goods.length>0"> <h1>购物车</h1> <table border="1" width="90%"> <tr> <th>编号</th> <th>名称</th> <th>价格</th> <th>数量</th> <th>总计</th> <th>操作</th> </tr> <tr v-for="v in goods"> <td>{{v.id}}</td> <td>{{v.title}}</td> <td>{{v.price}}</td> <td> <input type="text" v-model="v.num"> </td> <td>{{v.totalPrice}}</td> <td> <button @click="del(v.id)">删除</button> </td> </tr> </table> </div> </div> </script> <script type="text/x-template" id="footerCart"> <div class="footerCart"> <div v-if="totalPrice>0"> 总计: {{totalPrice}} </div> </div> </script> <style> .footerCart { background: #999; color: #fff; } </style> <script> let Lists = { template: '#Lists', computed: { goods(){ // console.log(this.$store.state.cart.goods); return this.$store.getters['cart/goods'] } }, methods: { del(id){ this.$store.commit('del', {id}) } } } let footerCart = { template: '#footerCart', computed: { totalPrice(){ return this.$store.getters['cart/totalPrice']; } } } const cartModule = { namespaced:true, state: { goods: [{id:12}] }, getters: { //获取商品总价 totalPrice: state => { let totalPrice = 0; state.goods.forEach((v) => { totalPrice += v.num * v.price; }) return totalPrice; }, //获取商品并计算每件商品的总价 goods(state){ let goods = state.goods; goods.forEach((v) => { v.totalPrice = v.num * v.price; }); return goods; } }, mutations: { //删除购物车中的商品 del(state, param){ let k; for (let i = 0; i < state.goods.length; i++) { if (state.goods[i].id == param.id) { k = i; break; } } state.goods.splice(k, 1); }, setGoods(state,param){ state.goods=param.goods; } }, actions: { loadGoods(store){ axios.get('73.php').then(function (response) { store.commit('setGoods',{goods:response.data}) console.log(response); }); } } } let store = new Vuex.Store({ modules:{ cart:cartModule } }); var app = new Vue({ el: '#hdcms', store, components: { Lists, footerCart }, mounted(){ this.$store.dispatch('cart/loadGoods'); } }) </script>