2

I try to bind attributes and some data to my template but the code below doesn't work. What I need is to render n-amount of templates depends of amount of printedForms objects and implement in each template data from proper object. Please give any ideas what wrong with my code.

P.S. I've warning in console as follows: [Vue warn]: Error when evaluating expression "printedForm.docNumber": TypeError: Cannot read property 'docNumber' of undefined (found in component: )

 <div id="app">
  <printing-form v-for="printedForm in printedForms" track-by="id"></printing-form>
 </div>

 <template id="printingForm-template">
   <img v-bind="printedForm.attributes">
   <div>{{ printedForm.docNumber }}</div>
 </template>

My VueJs code below:

Vue.component('printing-form', {
  template: '#printingForm-template'
});

new Vue({
  el: '#app',
  data: {
    printedForms: [
      {
        id: 1,
        docNumber: 7,
        attributes: {
          src: '/waybill/img/4p_bus.png',
          width: 1400,
          height: 980
        }
      },
      {
        id: 2,
        docNumber: 7777,
        attributes: {
          src: '/waybill/img/4p_cargo.png',
          width: 1400,
          height: 980
        }
      },
      {
        id: 3,
        docNumber: 10000,
        attributes: {
          src: '/waybill/img/4p_selfMove.png',
          width: 1400,
          height: 980
        }
      }
    ]
  }
});

1 Answer 1

7

you need to bind a printedForm property :printed-form="printedForm" like that

<printing-form v-for="printedForm in printedForms" 
track-by="id" :printed-form="printedForm"></printing-form>

and define it in component props

Vue.component('printing-form', {
  template: '#printingForm-template',
  props: ['printedForm']
});

Vue props

Notice when using camelCased prop names as attributes, you need to use their kebab-case (hyphen-delimited) equivalents

Vue Docs

Sign up to request clarification or add additional context in comments.

4 Comments

Beat me to it so I won't post my answer. Another thing I'd mention though it's not really related to the question is the component's template should have one root element.
no it shouldn't , it depend on what you are doing, i made that example check it vue exmple
Yes it should. In Vue 2 it's required. In Vue 1 you will end up with fragment instances and performance costs. v1.vuejs.org/guide/components.html#Fragment-Instance
@BillCriswell you are right , i found that in the Migration from Vue 1.x

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.