0

I use VueJS (in Laravel) to perform simple loop with variable's value.

Here, value comes from the template props.

HTML:

 <template id="segment" t_nb=2></template>


 <div id="test_loop">
  <b>@{{ t_nb }}</b>
  <div v-for="a in t_nb">
    <seg>@{{ a }}</seg>
  </div>
 </div>

VueJS:

Vue.component(
 'seg', {
    template: '#segment',
    props: ['t_nb']
      }
   );

OUTPUT:

 <!-- (Empty)-->

Here, i cant get loop for 2 times.

If i pass value of "t_nb" as 2 in direct manner like,

 <div v-for="a in 2">
    <seg>@{{ a }}</seg>
  </div>

then, i get OUTPUT as,

 1
 2

But i pass same value on both, vuejs only accepts the direct assignment.

Whats wrong with my code ?

How is this possible or any other solutions ?

3 Answers 3

1

Try to specify your t_nb props as an array, eg:

props: { 't_nb': { type: Array, default: [1, 2], // Value that you want to loop } }

And, then if you want to assign a new value for t_nb props, do:

<seg :t_nb="[3,4,5,6]"></seg>

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

Comments

1

Finally i find out my mistake.

Here, the loop having trouble to find it's datatype.

  <div v-for="a in t_nb">
    <seg>@{{ a }}</seg>
  </div>

to

  <div v-for="a in parseInt(t_nb)">
    <seg>@{{ a }}</seg>
  </div>

Now its works charm.

Issue is to specify the Datatype.

Thank to @saurabh.

Thank You all !

Comments

0

It seems there are two things wrong with your code.

First thing: You have to include you HTML in the template, as following. As you component seg is taking template '#segment', it has to enclose all the relevant HTML.

<template id="segment">

   <div id="test_loop">
    <b>@{{ t_nb }}</b>
    <div v-for="a in t_nb">
      <seg>@{{ a }}</seg>
    </div>
   </div>
</template>

Second thing: You have to pass props: t_nb=2 where you are rendering seg component, it should be like following:

<seg tn_b=2></seg>

2 Comments

but, why not variable ?
because v-for searches the variables used inside vue instance. You are also having t_nb as props.

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.