首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在计算函数中设置索引

在计算函数中设置索引
EN

Stack Overflow用户
提问于 2021-09-17 06:58:47
回答 1查看 47关注 0票数 0

我有一个v-for循环,如下所示:

代码语言:javascript
复制
<div class="inputArea mt-2" v-for="(id, index) in inputs" :key="index">
  <div class="row">
    <div class="col-md-6 m-1">
      <div class="mt-2">Input Number</div>
      <b-form-input type="number" v-model="Number[index]"></b-form-input>
    </div>
    <div class="row">
      <div class="col-md-5 ml-1 mr-1">
        <div class="mt-2">Autofill 1</div>
        <b-form-input type="text" :value="Autofill[index].autofill1" </b-form-input>
      </div>
      <div class="col-md-5 ml-1 mr-1">
        <div class="mt-2">Autofill 2</div>
        <b-form-input type="text" :value="Autofill[index].autofill2" </b-form-input>
      </div>
    </div>
  </div>
</div>

如何将我的索引引用到计算函数中:

代码语言:javascript
复制
 computed: {
    Autofill() {      <!-- Autofill[index]() is not working -->
        var returnelement = {};
        if(this.json!= undefined) {
          this.json.forEach(element => {
            for(const item of this.Number) {
              if (+element.number === +item) 
              returnelement = element;
            }
          }); 
        }
        return returnelement;
      }, 
  },

 data() {
      return {
        inputs:[{}],
        Number: [],
        json: json, //imported before
      }
  }

Autofillindex不起作用,但我需要在这里使用这个唯一的索引。

谢谢你帮我的忙!

EN

回答 1

Stack Overflow用户

发布于 2021-09-17 07:33:37

尝尝这个

如果要将索引从循环传递到Autofill,则应使用以下方法。不能将数据从computed传递给函数。

代码语言:javascript
复制
<div class="inputArea mt-2" v-for="(id, index) in inputs" :key="index">
  <div class="row">
    <div class="col-md-6 m-1">
      <div class="mt-2">Number</div>
      <b-form-input type="number" v-model="Number[index]"></b-form-input>
    </div>
    <div class="row">
      <div class="col-md-5 ml-1 mr-1">
        <div class="mt-2">Autofill 1</div>
        <b-form-input type="text" :value="Autofill(index).autofill1" </b-form-input>
      </div>
      <div class="col-md-5 ml-1 mr-1">
        <div class="mt-2">Autofill 2</div>
        <b-form-input type="text" :value="Autofill(index).autofill2" </b-form-input>
      </div>
    </div>
  </div>
</div>

<script>
export default {
  //...
  methods: {
    Autofill(index) {
      var returnelement = {};
      if (this.json != undefined) {
        this.json.forEach(element => {
          for (const item of index) {
            if (+element.number === +item)
              returnelement = element;
          }
        });
      }
      return returnelement;
    },
  },
}
</script>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69219054

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档