Vue中购物车

Vue中购物车是用axios进行调接口,然后放在store中,就可以随时调用!

示例:

Show.vue:

 

<template>
  <div class="header0">
    <table>
      <caption>
        商品界面
      </caption>
      <thead>
        <tr>
          <th>编号</th>
          <th>名称</th>
          <th>生产时间</th>
          <th>失效时间</th>
          <th>图片</th>
          <th>价格</th>
          <th>折扣</th>
          <th>购买</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(v, i) in ps" :key="i + 'link'">
          <td>{{ v.id_coach }}</td>
          <td>{{ v.name_coach }}</td>
          <td>{{ v.dage_coach }}</td>
          <td>{{ v.tage_coach }}</td>
          <td><img :src="baseurl + v.path_coach" alt="" class="img1" /></td>
          <td>{{ v.type_coach }}</td>
          <td>{{ v.honor_coach }}</td>
          <td><input type="button" value="购买" @click="buy2(i)" /></td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

<script>
import axios from "axios";
import qs from "qs";

export default {
  name: "Show",
  data() {
    return {
      ps: [],
      baseurl: "http://www.qhdlink-student.top/",
    };
  },
  computed: {
    // ps: function () {
    //   return this.$store.state.pss;
    // },
  },

  mounted() {
    axios({
      url: "http://www.qhdlink-student.top/student/coacha.php",
      method: "POST",
      data: { username: "cty", userpwd: 12345678, userclass: 67, type: 2 },
      transformRequest: [
        function (data) {
          return qs.stringify(data);
        },
      ],
      headers: { "Content-Type": "application/x-www-form-urlencoded" },
    }).then((value) => {
      for (let i in value.data) {
        this.ps.push(value.data[i]);
      }
      this.setpss(value.data);
    });
  },
  methods: {
    buy2: function (a) {
      this.$store.commit("buy1", a);
    },
    setpss(a) {
      this.$store.commit("changepss", a);
    },
  },
};
</script>

<style>
.header0 {
  width: 50%;
  height: 750px;
  float: left;
  background: rgb(241, 240, 181);
}
.img1 {
  width: 100px;
  height: auto;
}
</style>

vuex.js:

 

import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);

export var store = new Vuex.Store(
    {
        state: {
            buypss: [],
            pss: [],
            psss: []
        },
        mutations: {
            changepss(state, a) {
                for (let i in a) {
                    state.pss.push(a[i]);
                }
            },
            changepsss(state, a) {
                for (let i in a) {
                    state.psss.push(a[i]);

                }
            },
            addbuy(state, a) {
                state.buypss[a].num += 1;
                let b = state.buypss[a];
                state.buypss.splice(a, 1, b);
            },
            delbuy(state, a) {
                if (state.buypss[a].num == 1) {
                    state.buypss[a].disabled = true;
                } else {
                    state.buypss[a].num -= 1;
                    let c = state.buypss[a];
                    state.buypss.splice(a, 1, c);
                }
            },
            del(state, a) {
                state.buypss.splice(a, 1);
            },
            buy1: function (state, a) {
                let true_false = true;
                state.buypss.map((v, i) => {
                    if (v.id_coach == state.pss[a].id_coach) {
                        true_false = false;
                        v.num += 1;
                        state.buypss.splice(i, 1, v);
                    }
                })
                if (true_false) {
                    state.pss[a].num = 1;
                    state.buypss.push(state.pss[a]);
                }

            },
        },
    }
)

buy.vue:

 

<template>
  <div class="buy">
    <table>
      <caption>
        购物车界面
      </caption>
      <thead>
        <tr>
          <th>编号</th>
          <th>名称</th>
          <th>生产时间</th>
          <th>失效时间</th>
          <th>图片</th>
          <th>价格</th>
          <th>折扣</th>
          <th>购买</th>
          <th>移除</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(v, i) in buypss" :key="i + 'link'">
          <td>{{ v.id_coach }}</td>
          <td>{{ v.name_coach }}</td>
          <td>{{ v.dage_coach }}</td>
          <td>{{ v.tage_coach }}</td>
          <td><img :src="baseurl + v.path_coach" alt="" class="img1" /></td>
          <td>{{ v.type_coach }}</td>
          <td>{{ v.honor_coach }}</td>
          <td>
            <input type="button" value=" + " @click="changeadd(i)" />
            {{ v.num }}
            <input
              type="button"
              value=" - "
              v-if="v.num == 1"
              disabled
              @click="changedel(i)"
            />
            <input type="button" v-else value=" - " @click="changedel(i)" />
          </td>
          <td><input type="button" value="移除" v-on:click="del(i)" /></td>
        </tr>
      </tbody>
    </table>
    <h4>总价格:{{ price }}</h4>
    <input type="button" value="点击结算" />
  </div>
</template>
<script>
export default {
  name: "Buy",
  data() {
    return {
      baseurl: "http://www.qhdlink-student.top/",
    };
  },
  computed: {
    buypss() {
      return this.$store.state.buypss;
    },
    price() {
      let d = 0;
      this.$store.state.buypss.map((v, i) => {
        d += v.num * v.type_coach;
      });
      return d;
    },
  },
  methods: {
    changeadd(a) {
      this.$store.commit("addbuy", a);
    },
    changedel(a) {
      this.$store.commit("delbuy", a);
    },
    del(a) {
      this.$store.commit("del", a);
    },
  },
};
</script>
<style scoped>
.buy {
  width: 50%;
  height: 750px;
  float: left;
  background: rgb(120, 119, 221);
}
</style>

最终效果图:

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值