【问题标题】:Using v-for to show dynamic slides in VuejsVuejs中使用v-for显示动态幻灯片
【发布时间】:2019-10-12 01:06:25
【问题描述】:

使用 Vue Carousel(https://ssense.github.io/vue-carousel/) 从索引文件中的节点列表构建幻灯片的自定义 Vue 组件遇到了一些问题。一些轮播数据是通过data.json 文件读入的,但我想基于包含节点列表的对象创建一个新幻灯片。所以我相信我需要我们 v-for 然后遍历该对象,为节点列表中的每个实例创建一个<slide></slide>

NodeList 是在mounted() lifehook 中创建的,但我不知道如何将它绑定到组件,而且对Vue 来说还是很新的,我没有构建这个模板系统。任何帮助,将不胜感激!

import { Carousel, Slide } from 'vue-carousel';
let articles={};

export default {
    name: 'ProductCarousel',
    props: {
      dnode: Object,
      value: Object
    },
    components: { 
        Carousel,
        Slide    
    },
    data() {
      return {
        carousel: this.dnode,
        product: articles
      }
    },
    mounted() {
      var _self = this;
      var rowName = ".js-ep--" + _self.carousel.anchor;
      let e4pRow = document.querySelector(rowName);
      var productRows;

      if (e4pRow && !window.isEditMode) {
        productRows = e4pRow.querySelectorAll(".product-grid");
  
        if (productRows) {
          for (var len = productRows.length, i = 0; i < len; i++) {
            articles[i] = productRows[i].querySelectorAll("article");
            //console.log(articles[i]);
          }
        //console.log(articles);
        }
      }
    }
    
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<template>
<section :id="carousel.anchor" :class="carousel.classList" >
  
    <h1 v-if="carousel.header" v-text="carousel.header" />
    <h2 v-if="carousel.subheader" v-text="carousel.subheader"/>
    <p v-if="carousel.paragraphText" v-text="carousel.paragraphText" />
    <carousel 
    :navigationEnabled="carousel.navigation"
    :paginationEnabled="carousel.pagination"
    :navigationNextLabel="carousel.navigationNextIcon"
    :navigationPrevLabel="carousel.navigationPrevIcon"
    :navigationClickTargetSize="carousel.arrowClickSize"
    :perPageCustom="[
    [640, carousel.numSlidesSmall], 
    [1024, carousel.numSlidesMedium],
    [1920, carousel.numSlidesLarge]]">
        <slide></slide>
    </carousel>
  </section>
</template>

【问题讨论】:

  • 您可以在模板中使用的每个变量都必须属于 Vue 实例。要将变量添加到 Vue 实例,您必须首先将它们声明为数据对象的属性,然后使用 this.yourVariable 语法为它们赋值。
  • 哦,谢谢!我会试试看!请原谅我的简短,第一篇关于 Vue 的帖子,实际上是在 Stackoverflow 中...@Ayrton ????

标签: javascript vue.js components v-for


【解决方案1】:
import { Carousel, Slide } from 'vue-carousel';
let articles={};

export default {
    name: 'ProductCarousel',
    props: {
      dnode: Object,
      value: Object
    },
    components: { 
        Carousel,
        Slide    
    },
    data() {
      return {
        carousel: this.dnode,
        product: []
      }
    },
    mounted() {
      var _self = this;
      var rowName = ".js-ep--" + _self.carousel.anchor;
      let e4pRow = document.querySelector(rowName);
      var productRows;

      if (e4pRow && !window.isEditMode) {
        productRows = e4pRow.querySelectorAll(".product-grid");

        if (productRows) {
          for (var len = productRows.length, i = 0; i < len; i++) {
            articles[i] = productRows[i].querySelectorAll("article");
            //console.log(articles[i]);
          }
        //console.log(articles);
          this.product = articles; //loop through product in your template
        }
      }
    }

}

【讨论】:

  • 我尝试使用类似 thist:&lt;slide v-bind="{value}" v-for="value in products" :key="value.id"&gt; &lt;/slide&gt; 的东西,但这不是根据捕获的节点列表创建幻灯片,不确定我做错了什么
  • 我认为我正确地使用了生命周期挂钩。也许需要在安装前使用渲染功能?找到this guide
猜你喜欢
  • 1970-01-01
  • 2021-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-17
  • 1970-01-01
相关资源
最近更新 更多