2

I've an issue to fill data from model into code when I'v more then one template with separate data. What I need is first template renders as many times as many objects in firstFormDetails array and same the second one. There example of my code below:

 <div id="app">
  <first v-for="item in secondFormDetails" track-by="id"></first>
  <second v-for="item in firstFormDetails" track-by="id"></second>
 </div>

 <template id="template-first">
  <div> {{ item.docNumber }}</div>
 </template>

 <template id="template-second">
  <div> {{ item.docNumber }}</div>
 </template>

And VueJs module as follows:

  Vue.component('first', {
     template: '#template-first',
     data: function() {
       return {
         firstFormDetails: [
           {docNumber: 7},
           {docNumber: 7777},
           {docNumber: 10000}
         ]
       }
     }
   });

   Vue.component('second', {
      template: '#template-second',
      data: function() {
        return {
          secondFormDetails: [
            {docNumber: 1908},
            {docNumber: 7777},
            {docNumber: 10000}
          ]
        }
      }
    });

   new Vue({
     el: '#app'
   });
3
  • 1
    You are referencing the data incorrectly. Your data needs to be located in the parent component to use v-for Commented Oct 9, 2016 at 18:32
  • I need to split data between components because I have different HTML for each template and different data should be loaded via AJAX. I found an example her - v1.vuejs.org/guide/components.html#Component-Option-Caveats Commented Oct 9, 2016 at 20:06
  • 1
    try adding a prop to each component and binding the value to it. your prop would be item and the binding would look like :item="item" in your v-for, and in each component you would add props: ['item']. alternately a better solution is to move the v-for to the child component where the data is actually located or move the data up to the parent component Commented Oct 9, 2016 at 21:42

5 Answers 5

6

@vbranden is correct, move the v-for into the component

 <template id="template-first">
  <div v-for="item in firstFormDetails"> {{ item.docNumber }}</div>
 </template>

 <template id="template-second">
  <div v-for="item in secondFormDetails"> {{ item.docNumber }}</div>
 </template>
Sign up to request clarification or add additional context in comments.

Comments

1

You must define what components you use. Let try to use this:

 var first = Vue.component('first', {
   template: '#template-first',
   data: function() {
     return {
       firstFormDetails: [
         {docNumber: 7},
         {docNumber: 7777},
         {docNumber: 10000}
       ]
     }
   }
 });

 var second = Vue.component('second', {
   template: '#template-second',
   data: function() {
      return {
        secondFormDetails: [
          {docNumber: 1908},
          {docNumber: 7777},
          {docNumber: 10000}
        ]
      }
   }
 });

new Vue({
  el: '#app',
  components: {
    'first': first,
    'second': second
  }
});

1 Comment

Vue.component registers a global component. It does not need to be included in the Vue instance
0

in addition on @johnnynotsolucky 's answer, you will need a wrapper element out of v-for, since only allow only one element inside it.

<template id="template-first">
  <div class="form-details">
    <div v-for="item in firstFormDetails"> {{ item.docNumber }}</div>
  </div>
</template>

Comments

0

var first = Vue.component('first', {
  template: '#template-first',
  props: ['item']
});

var second = Vue.component('second', {
  template: '#template-second',
  props: ['item']
});

new Vue({
  el: '#app',
  components: {
    'first': first,
    'second': second
  },
  data: function () {
    return {
      firstFormDetails: [
        {docNumber: 7},
        {docNumber: 7777},
        {docNumber: 10000}
      ],
      secondFormDetails: [
        {docNumber: 1908},
        {docNumber: 7777},
        {docNumber: 10000}
      ]
    }
  }
});
<div id="app">
  <first v-for="item in secondFormDetails" :item="item"></first>
  <second v-for="item in firstFormDetails" :item="item"></second>
</div>

<template id="template-first">
  <div> {{ item.docNumber }}</div>
</template>

<template id="template-second">
  <div> {{ item.docNumber }}</div>
</template>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.3.3/vue.min.js"></script>

Comments

0

When Vue app is loaded, it looks for component field, if component field is not defined then not component is loaded, if component field is defined, Vue looks for the definition of the component and parse it for syntax checking, once the syntax is correct the component is binded. This happens recursively for nested components.

Registering a component is mandatory

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.