简体   繁体   English

如何从 v-for (vue.js) 读取数据?

[英]How to read data from a v-for (vue.js)?

Given the next v-for:给定下一个 v-for:

  <div class="container-fluid" id="networdapp" style="display:none;">
     <div class="row" >
       <div v-for="result in results" class="col-sm-6" >
         <div class="card m-3 h-240  bg-light" >
         <div class="card-header text-center" > {{ result.title }} </div>
           <div class="card-body" style="height:200px" >
             <p class="card-text" v-html="result.prevDesc"></p>
           </div>
             <div class="card-footer bg-transparent border-info">
               <a href="/details" class="btn btn-info" @click="getData(result)" >Details</a>
             </div>
         </div>
      </div>
      </div>
     </div>

And the next Vue.js script:下一个 Vue.js 脚本:

   <script type="text/javascript">
  const vm = new Vue({
   el: '#networdapp',
    data: {
     results:[]
    },

    methods: {

    getData: function(result){
    window.alert($(this).parents("#networdapp").find(".card-header.text-center").outerHTML);
    window.alert(document.getElementsByClassName("card-header").outerHTML);
    window.alert(result.outerHTML);
     }

    },

     mounted() {
   axios.get('/getJson')
     .then(response => {
    this.results = response.data;
    })
      .catch( e => {
   console.log(e);
     });
     }
   });

     </script>

I want to get data from a specific iteration,let's say if I click the "Details" button of the 3rd div from the v-for I want to get the {{result.title }} data from the 3rd for.Is it possible?I've been reading the Vue.js documentation but I didn't find anything about reading the data from DOM.If it is not possible,than how can I do that without Vue.js?Is there any other option?我想从特定迭代中获取数据,假设我从 v-for 中单击第 3 个 div 的“详细信息”按钮,我想从第 3 个 for 中获取 {{result.title }} 数据。是否可能?我一直在阅读 Vue.js 文档,但我没有找到任何关于从 DOM 读取数据的信息。如果不可能,那么没有 Vue.js 我怎么能做到这一点?还有其他选择吗? The main goal is to get this data and to put it into a js object passing it to another webpage.主要目标是获取这些数据并将其放入一个 js 对象中,并将其传递给另一个网页。

you have to pass index key and use is to get from results 's position.您必须传递索引键并使用 is 从results的位置获取。

change the for loop div into将 for 循环 div 更改为

<div  v-for="(result,i) in results" :key="i" class="col-sm-6" >

also chnange the methods parameter也改变方法参数

<a href="/details" class="btn btn-info" @click="getData(i)" >Details</a>

and the method will get the index key and here i have used console to see the result.title that you have wanted.并且该方法将获取索引键,在这里我使用控制台查看了您想要的 result.title。 you can use it any how you want.你可以随心所欲地使用它。

getData: function(key){
console.log(this.results[key].title)
 }

so Given the next v-for:所以给定下一个 v-for:

 <div class="container-fluid" id="networdapp" style="display:none;">
     <div class="row" >
       <div  v-for="(result,i) in results" :key="i" class="col-sm-6" >
         <div class="card m-3 h-240  bg-light" >
         <div class="card-header text-center" > {{ result.title }} </div>
           <div class="card-body" style="height:200px" >
             <p class="card-text" v-html="result.prevDesc"></p>
           </div>
             <div class="card-footer bg-transparent border-info">
               <a href="/details" class="btn btn-info" @click="getData(i)" >Details</a>
             </div>
         </div>
      </div>
      </div>

And the next Vue.js script:下一个 Vue.js 脚本:

<script type="text/javascript">
  const vm = new Vue({
   el: '#networdapp',
    data: {
     results:[]
    },

    methods: {

    getData: function(key){
    console.log(this.results[key].title)
     }

    },

     mounted() {
   axios.get('/getJson')
     .then(response => {
    this.results = response.data;
    })
      .catch( e => {
   console.log(e);
     });
     }
   });

To get the data you want to access in the results array, you can use an index in your v-for loop要在结果数组中获取要访问的数据,可以在 v-for 循环中使用索引

v-for="(result, index) in results"

you can check the docs here https://v2.vuejs.org/v2/guide/list.html你可以在这里查看文档https://v2.vuejs.org/v2/guide/list.html

I also strongly recommend you to add a key attribute after the v-for to help vue.js keep track of each result, see https://v2.vuejs.org/v2/guide/list.html#key我也强烈建议你在 v-for 之后添加一个 key 属性来帮助 vue.js 跟踪每个结果,请参阅https://v2.vuejs.org/v2/guide/list.html#key

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

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