采坑uni-app自定义组件数组渲染问题帮主更多有需要的人

本文介绍了在Vue中如何创建一个用于显示和编辑快递单号的子组件,该子组件接收父组件传递的数组并进行回显、修改和新增功能。在实现过程中遇到了子组件数据更新但视图未更新的问题,通过在`mounted`生命周期钩子中初始化数据并监听`input`事件来解决。同时,提供了父组件如何监听子组件的变更并更新数据的示例。
摘要由CSDN通过智能技术生成

业务简单介绍,封装一个数组 快递单号 子组件,父组件操作引用,并具有回显修改新增功能

使用场景:

        父组件的数组给到子组件用,父组件的请求回显的数据给子组件,子组件更新给父组件。

所遇到的问题:

        子组件新增一条数据,删除数据视图无更新,数据有变化

   watch无效,重点解决方法mounted,代码垃圾,实现功能就好

话不多说直接贴代码

子组件


<template>
  <view class="fillEMail">
    <view class="fillEMail-head">
      <view class="fillEMail-head-left">
        填写邮件资料快递编号
      </view>
      <view class="fillEMail-head-right">
        <u-button type="warning" :custom-style="customStyle" @click="add" size="mini">添加</u-button>
      </view>
    </view>
    <view class="fillEMail-txt" v-for="(item,index) of items2" :key="index">
      <u-field v-model="items2[index]" label="快递单号" @blur="input" placeholder="请填写邮寄资料的快递编号">
        <u-button slot="right" size="mini" @click="del(index)">删除</u-button>
      </u-field>
    </view>
  </view>
</template>
<script>
  export default {
    name: "courierNumber",
    // props:["items"],
    props: {
      items: Array,
    },
    data() {
      return {
        items2: [], //input控件
        zlkddh: '',
        // items:[],
      };
    },
    mounted() {
      var _this = this;
      _this.items2 = _this.items
      console.log('看看这是什么呀', _this.items2)
    },
    methods: {
      //光标事件
      input() {
        this.items = this.items2
        this.$emit('change', this.items)
      },
      //添加input控件
      add() {
        console.log('11', this.items)
        this.items2.push('')
      },
      //删除input控件
      del(index) {
        console.log('11', this.items2)
        this.items2.splice(index, 1);
      },
    }
  }
</script>

ru父组件

    <!-- 快递编号 -->
    <courierNumber @change="change" :items="items"></courierNumber>

methods:{
    change(res) {
        console.log(res)
        this.zlkddh = res.toString()
      },
}
     

如有高手请多指教!!
这里后端返回的是字符串我转的数组传递,和生成字符串转数组传递,不影响使用,去掉即可。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ㅤ零到一

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值