Vue监听一个数组id是否与另一个数组id相同

本文介绍如何在Vue中监听两个数组(list和shoplist)的ID,当shoplist中的ID与list中的ID相同时,展示对应list的数据。通过.js和.vue文件的代码示例,展示了在初始状态及ID变化后的效果。

数据list,结构为[{id:1,…},{id:2,…}],数据shoplist,结构为[{id:1,…},{id:2,…}],判断当shoplist.id等于list.id时显示list的数据。

.vue文件:

<template>
  <div class="hello">
    <div class="shoplist">
      <button @click="clickButtonShopList">click me</button>
      <span>shoplist-id:</span><input type="text" v-model="shoplist[shopCount].id">
    </div>
    <div class="list">
      <button @click="clickButtonList">click me</button>
      <span>list-id:</span><input type="text" v-model="list[listCount].id">
    </div>

    <input class="data" v-model="data">

    <h2>{{list}}</h2>
    <h2>{{shoplist}}</h2>
  </div>
</template>

.js文件:

export default {
  data () {
    return {
      msg: 'Welcome to Your Vue.js App',
      shopCount:0,
      listCount:0,
      data:'',
      list:[{id:1,name:'hello'},{id:2,name:'hello'},{id:3,name:'hello'},{id:4,name:'hello'},{id:5,name:'hello'}],
      shoplist:[{id:1,name:'hello'},{id:2,name:'hello'},{id:3,name:'hello'},{id:4,name:'hello'},{id:5,name:'hello'}]
    };
  },
  methods:{
  clickButtonShopList:function () {
      this.shopCount++;
      this.shopCount=this.shopCount%this.shoplist.length;
      this.getData();
    },
    clickButtonList:function () {
      this.listCount++;
      this.listCount=this.listCount%this.list.length;
      this.getData();
    },

    getData:function () {
      this.data='';
      if(this.shoplist[this.shopCount].id===this.list[this.listCount].id){
//        this.data=this.list;
        this.data=this.list.map((element)=>{
          return element.id+element.name;
        }).join(',')
      }else {
        this.data='';
      }
    }
  },
  mounted: function(){
    this.getData();
  }
}

.less文件:

.list{
    margin-top: 20px;
  }

  .data{
    width: 500px;
    height:200px;
    border: 1px solid #666666;
    margin-top: 20px;
  }

效果:
刚开始时:
这里写图片描述

两边id不同时:
这里写图片描述

通过点击使得两边id相同时:
这里写图片描述

Vue 3 TypeScript 结合的项目中,监听数组变化主要依赖于 `ref` 和 `watch` 的组合使用。由于数组是引用类型,直接监听数组变量只能检测到整个数组被替换的情况,而无法检测到数组内部元素的变化。因此需要采用深度监听(deep watching)或监听数组的响应式包装器。 ### 使用 `watch` 监听数组变化 #### 1. 监听整个数组被替换的情况 当数组本身被重新赋值时,可以使用以下方式监听: ```typescript import { ref, watch } from &#39;vue&#39;; const numbers = ref<number[]>([1, 2, 3]); watch( () => numbers.value, (newVal, oldVal) => { console.log(&#39;数组被替换:&#39;, newVal, oldVal); } ); ``` 此方式仅能检测到 `numbers.value = [...]` 这种整体赋值操作,无法检测到数组内部元素的修改(如 `push`、`splice` 等方法)[^1]。 #### 2. 深度监听数组内部元素变化 如果希望监听数组内部元素的变化(例如通过 `push` 或 `splice` 修改内容),需要开启深度监听: ```typescript watch( () => numbers.value, (newVal, oldVal) => { console.log(&#39;数组内容发生变化:&#39;, newVal, oldVal); }, { deep: true } ); ``` 这样即使数组的某个元素发生变化,也可以触发回调函数。 #### 3. 使用 computed 包装监听条件 如果只需要监听数组中的某些特定字段(如对象数组中某个属性),可以结合 `computed` 来提取关注的字段进行监听: ```typescript interface User { id: number; name: string; } const users = ref<User[]>([ { id: 1, name: &#39;Alice&#39; }, { id: 2, name: &#39;Bob&#39; } ]); watch( () => users.value.map(user => user.name), (newNames, oldNames) => { console.log(&#39;用户名称发生变化:&#39;, newNames, oldNames); } ); ``` 这种方式避免了不必要的深度监听,提高了性能[^3]。 ### 注意事项:Vue 3 数组响应性限制 Vue 3 使用 Proxy 实现响应式系统,但对数组的操作仍然存在一些限制。例如,直接通过索引修改数组元素(如 `arr[0] = newValue`)不会触发视图更新。应优先使用 Vue 提供的响应式方法,如 `push`、`pop`、`splice` 等来确保变更可被正确追踪[^5]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值