简体   繁体   English

Vue 向@click 事件添加条件

[英]Vue adding condition to @click event

I have a bootstrap collapse and I want to perform a fetch of data when you open the collapse but not when you close it.我有一个引导程序折叠,我想在打开折叠时而不是在关闭它时执行数据提取。 So I need to perform a check on the aria-expanded attribute.所以我需要对aria-expanded属性进行检查。

I am not sure if this is a good approach, because this will always perform the fetch, on close and on open.我不确定这是否是一个好方法,因为这将始终在关闭和打开时执行提取。

<span
    data-toggle="collapse"
    data-target="#collapseExample"
    aria-expanded="false"
    aria-controls="collapseExample"
>
    <small @click="fetch(id)">Open/Close</small>
</span>

You can have your logic in the fetch method to check if the data is already loaded.您可以在 fetch 方法中使用逻辑来检查数据是否已加载。 Add a new dictionary to the data() and add the details to the fetchedData .将新字典添加到 data() 并将详细信息添加到fetchedData

data() {
  return {
    fetchedData: {}
  }
},
methods: {
   fetch: function(id) {
       if(this.fetchedData[id]) {
          return this.fetchedData[id];
       } else {
          // call to fetch the data and add those details to the dictionary fetchedData
          this.fetchedData[id] = response.data;
       }
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM