Vuejs v-bind v-for 未填充到组件模板中

huangapple go评论69阅读模式
英文:

Vuejs v-bind v-for not populating into component template

问题

以下是代码的翻译部分,代码中的HTML标记和Vue组件部分已经翻译好了:

  <div id="app">
    <gallery v-bind:links="['link1.jpg', 'link2.jpg']" />
  </div>

和组件定义:

Vue.component('gallery', {
  template: `
    <div v-if="!isReady">
    not ready
    </div>
    <div v-else>
      <image-grp v-for="src in links">
        <img v-bind:src="src">
      </image-grp>
    </div>
  `,
  data() {
    return {
      links: [],
      isReady: false
    }
  },
  mounted() {
    this.links = this.$el.getAttribute('links').split(',')
    this.isReady = true
    console.log(this.links);
  }
});

如果您有其他问题或需要进一步的帮助,请随时告诉我。

英文:

with html

  <div id="app">
    <gallery v-bind:links="['link1.jpg', 'link2.jpg']" />
  </div>

and component definition:

    Vue.component('gallery', {
      template: `
        <div v-if="!isReady">
        not ready
        </div>
        <div v-else>
          <image-grp v-for="src in links">
            <img v-bind:src="src">
          </image-grp>
        </div>
      `,
      data() {
        return {
          links: [],
          isReady: false
        }
      },
      mounted() {
        this.links = this.$el.getAttribute('links').split(',')
        this.isReady = true
        console.log(this.links);
      }
    });

I've managed to produce this html:

<div links="link1.jpg,link2.jpg">
  <div class="image">
    <img src="">
  </div>
  <div class="image">
    <img src="">
  </div>
</div>

The images are not showing up because of the empty src. The src should be filled in during the loop on src in links. The array links must be filled properly, cause the html shows the same number of image-grp elements as are in the links array.

I have tried a variety of ways to populate/bind/mustache the src into the dynamic <img src=""> elements. Any guidance would be appreciated.

答案1

得分: 1

我看不出你的代码有任何问题。它可以正常工作。

但是,最好将 links 作为 props,像这样:

props: ['links']

然后,其他被注释掉的行不再需要。

Playground

Vue.component('gallery', {
  props: ['links'],
  template: `
    <div v-if="!isReady">
    not ready
    </div>
    <div v-else>
      <image-grp v-for="src in links">
        <img v-bind:src="src">
      </image-grp>
    </div>
  `,
  data() {
    return {
      //links: [],
      isReady: false
    }
  },
  mounted() {
    //this.links = this.$el.getAttribute('links').split(',');
    this.isReady = true;
  }
});

const app = new Vue({
    el: '#app'
})
img {
  width: 100px;
  height: 100px;
  margin: 4px;
}
<div id="app">
   <gallery v-bind:links="['https://i.stack.imgur.com/NxnaT.jpg?s=256&g=1', 'https://www.gravatar.com/avatar/50309120892edf29dcb2188bdabe3b08?s=256&d=identicon&r=PG']"></gallery>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@^2.0.0/dist/vue.min.js"></script>
英文:

I don't see any problems with your code. It works.

But you should better make links to props, like this:

props: [&#39;links&#39;]

Then, the other commented out lines are not needed.

Playground

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

Vue.component(&#39;gallery&#39;, {
  props: [&#39;links&#39;],
  template: `
    &lt;div v-if=&quot;!isReady&quot;&gt;
    not ready
    &lt;/div&gt;
    &lt;div v-else&gt;
      &lt;image-grp v-for=&quot;src in links&quot;&gt;
        &lt;img v-bind:src=&quot;src&quot;&gt;
      &lt;/image-grp&gt;
    &lt;/div&gt;
  `,
  data() {
    return {
      //links: [],
      isReady: false
    }
  },
  mounted() {
    //this.links = this.$el.getAttribute(&#39;links&#39;).split(&#39;,&#39;);
    this.isReady = true;
  }
});

const app = new Vue({
    el: &#39;#app&#39;
})

<!-- language: lang-css -->

img {
  width: 100px;
  height: 100px;
  margin: 4px;
}

<!-- language: lang-html -->

&lt;div id=&quot;app&quot;&gt;
   &lt;gallery v-bind:links=&quot;[&#39;https://i.stack.imgur.com/NxnaT.jpg?s=256&amp;g=1&#39;, &#39;https://www.gravatar.com/avatar/50309120892edf29dcb2188bdabe3b08?s=256&amp;d=identicon&amp;r=PG&#39;]&quot;&gt;&lt;/gallery&gt;
&lt;/div&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/vue@^2.0.0/dist/vue.min.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月10日 06:59:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75405329.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定