vue弹出层,弹出层带着父组件参数网络请求

本文介绍了一种在Vue中父组件如何通过点击事件调用子组件并传递参数的方法,同时展示了子组件如何接收这些参数并进行网络请求,最后通过回调函数通知父组件关闭子组件的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

父组件,点击循环中的图片,响应事件:

         <div v-for="(order,index) in orders" :key="index" class="col">
          //……
            <img
              class="card-item__img"
              @click="clickEach(order.wxId)"
              :src="`https://www.网址.cn//${order.goodImg}`"
            />
          //……
         </div>
    // 单击每个图片,弹出每个订单的详情
    clickEach: function(value) {
      let that = this;

      that.orderDetailLayer = true;
      that.dialogOption = {
        wxId: value,
        title: "订单",
        text: "订单详情",
        cancelButtonText: "取消",
        confirmButtonText: "确认"
      };
    },


    // 接收子组件回调的值,关闭子组件
    childRe: function(value) {
      let that = this;
      // 关闭子组件
      that.orderDetailLayer = false;
    },

在子组件中,并没有在mounted()发起网络请求,因为这时候传递的参数还获取不到:


<script>
export default {
  name: "dialog",
  //接受父组件传递值
  props: {
    dialogOption: Object
  },
  data() {
    return {
      orders: {}
    };
  },
  mounted() {},
  computed: {
    modal: function() {
      let options = this.dialogOption;
      return {
        wxId: options.wxId,
        title: options.title || "提示",
        text: options.text || "",
        cancelButtonText: options.cancelButtonText
          ? options.cancelButtonText
          : "取消",
        confirmButtonText: options.confirmButtonText
          ? options.confirmButtonText
          : "确定"
      };
    }
  },
  watch: {
    //观察到modal发生变化,便是得到了参数,拿着参数去网络请求。
    modal(val) {
      let that = this;
      that.$axios
        .post("https://www.接口.com/ail", {
          orderId: val.wxId
        })
        .then(body => {
          console.log(body.data);

        })
        .catch(error => {
          console.log(error);
        });
    }
  },
  methods: {
    submitBtn: function(value) {
      //单击确定按钮时候,响应父组件中的childRe方法,关闭子组件。
      this.$emit("childRe", value);
    },
    cancelBtn: function(value) {
      this.submitBtn(value);
    }
  }
};
</script>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值