这篇文章主要介绍了Vue如何监听一个数组id是否与另一个数组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">
<h3>{{list}}</h3>
<h3>{{shoplist}}</h3>
</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如何监听一个数组id是否与另一个数组id相同”这篇文章对大家有帮助,同时也希望大家多多支持编程网,关注编程网行业资讯频道,更多相关知识等着你来学习!