简体   繁体   English

vue js可以在自定义组件模板中使用v-for吗?

[英]vue js is possible using v-for inside custom component template?

I found nothing around the web. 我在网上找不到任何东西。 What I was wondering is if it is possible to use the v-for directive inside the template of a custom component. 我想知道的是,是否有可能在自定义组件的模板内使用v-for指令。 Here is what I have done: 这是我所做的:

HTML HTML

        <div id="macroservizi" class="col-lg-12 col-xs-12 collapse" v-if="modello">
           <lista-servizi :servizi="modello"></lista-servizi>
        </div>

CUSTOM COMPONENT 定制组件

Vue.component('lista-servizi', {
template:
                '<div id="tuttiGialla" class="collapse">'+
                    '<template class="servizioInlista">'+
                        '<div class="row" v-for="(single,index) in servizi.lineaGialla.servizi" :key="single.nomeServizio">'+
                            '<div class="col-lg-8 col-xs-8 nopadding">'+
                                '<h4 class="blu">{{single.nomeServizio}} {{index}}</h4>'+
                            '</div>'+
                            '<div class="col-lg-4 col-xs-4>"'+
                                '<span class="pull-right nomargin" v-on:click="mostraServiziGiallo(index)" v-bind:class="[GiallaTutti ? \'minus\' : !GiallaTutti,\'plus\']" data-toggle="collapse" data-target="\'#singoloGialla-\'+index"></span>'+                            
                            '</div>'+
                        '</div>'+
                        '<div v-bind:id="\'#singoloGialla-\'+index" class="row">'+
                            '<p>{{single.descrizione}}</p>'+
                        '</div>'+
                    '</template>'+
                '</div>',
 props: ['servizi'],

computed: {
    listaServizi() {
        return this.servizi
    }
}

I have the following errors in the console: 控制台中出现以下错误:

- tag <template> has no matching end tag. found in ---> <ListaServizi>

vue.js:435 [Vue warn]: Property or method "index" is not defined on the instance but referenced during render. Make sure to declare reactive data properties in the data option.

found in

---> <ListaServizi>
   <Root>

[Vue warn]: Property or method "single" is not defined on the instance but referenced during render. Make sure to declare reactive data properties in the data option.

I tried multiple ways to render it, but at this point I wonder if it is even possible. 我尝试了多种渲染方法,但是现在我不知道是否可能。

First, v-for works fine in a custom component. 首先, v-for在自定义组件中可以正常工作。

Second, there is no need to use string concatenation to build your template. 其次,无需使用字符串连接来构建模板。 Use javascript template literals to wrap your template and you can use as many lines as you want. 使用javascript 模板文字来包装模板,您可以根据需要使用任意多行。

Here I have tried to clean up your component as best I can understand it. 在这里,我尽力了解您的情况,尝试清理您的组件。

Vue.component('lista-servizi', {
  template:`
    <div>
        <div id="tuttiGialla" class="collapse">
          <template v-for="(single,index) in servizi.lineaGialla.servizi">
            <div class="row servizioInlista" 
                 :key="single.nomeServizio">
              <div class="col-lg-8 col-xs-8 nopadding">
                <h4 class="blu">{{single.nomeServizio}} {{index}}</h4>
              </div>
              <div class="col-lg-4 col-xs-4>">
                <span class="pull-right nomargin" 
                      v-on:click="mostraServiziGiallo(index)" 
                      v-bind:class="[GiallaTutti ? 'minus' : 'plus']" 
                      data-toggle="collapse" 
                      data-target="'#singoloGialla-' + index">
                </span>                            
              </div>
            </div>
            <div v-bind:id="'#singoloGialla-' + index" class="row">
              <p>{{single.descrizione}}</p>
            </div>
          </template>
        </div>
    </div>  
  `,
  props: ['servizi'],
  computed: {
    listaServizi() {
      return this.servizi
    }
  },
  created(){
    console.log(this.servizi)
  }
})

The main issue here is you were referencing single and index in this part of the template: 这里的主要问题是您在模板的这一部分中引用了singleindex

<div v-bind:id="'#singoloGialla-' + index" class="row">
  <p>{{single.descrizione}}</p>
</div>

But that code was outside of your v-for . 但是该代码不在您的v-for I added the v-for to your template so that that bit of the template is now in scope for the loop iteration. 我将v-for添加到您的template以便该template该位现在在循环迭代的范围内。

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

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