简体   繁体   English

如何在 Vue.js 中使用 v-for 动态创建一个新的 div?

[英]How to dynamically create a new div using v-for in Vue.js?

I want to create div's dynamically based on the number of elements present in an array.我想根据数组中存在的元素数量动态创建 div。 The div's contain the html element created by ProgressBar.js. div 包含由 ProgressBar.js 创建的 html 元素。

This the Vue.js code这是 Vue.js 代码

 import ProgressBar from 'progressbar.js' var bar; export default { data() { return { fitness: ['Run', 'Cycle'], val: 0.65 } }, mounted(){ this.showProgressBar(this.val); }, created: function() { }, methods:{ showProgressBar: function(val){ new ProgressBar.Circle('#container',{ trailColor: 'gainsboro', trailWidth: 20, color: 'teal', strokeWidth: 20 }).animate(val); } } }
 <div class="content" v-for="fitness in fitness"> <span>{{ fitness }}</span> <div id="container"></div> </div>

Since an id is associated with only one div, I am not able to execute a new ProgressBar.Circle object that would create another div.由于 id 仅与一个 div 相关联,因此我无法执行会创建另一个 div 的新 ProgressBar.Circle 对象。 Is there a way to dynamically create a new div with different a id inside the v-for every time the new ProgressBar.circle is executed?每次执行新的 ProgressBar.circle 时,有没有办法在 v-for 中动态创建一个具有不同 id 的新 div? Can somenone please help me out here?有人可以帮我吗?

Here is a re-usable component that wraps progressbar.js .这是一个包装progressbar.js的可重用组件。

<template>
  <div class="container"><div ref="bar"></div></div>
</template>
<script>
  import ProgressBar from "progressbar.js"

  export default {
    props:["options", "val"],
    mounted(){
      new ProgressBar.Circle(this.$refs.bar, this.options).animate(this.val)
    } 
  }
</script>
<style>
  .container{
    width:100px; 
    height: 100px
  }
</style>

Here is how it's used in a template:以下是它在模板中的使用方式:

<div v-for="fit in fitness" class="fitness">
  <div>{{fit.name}}</div>
  <progress-bar :val="fit.val" :options="options"></progress-bar>
</div>

Working Example .工作示例

One solution could be give unique ids to each container div and create progress bars for each of them.一种解决方案是为每个容器 div 提供唯一的 id,并为每个容器创建进度条。

Try the code below -试试下面的代码 -

 import ProgressBar from 'progressbar.js' var bar; export default { data() { return { fitness: ['Dietary Intake', 'Exercise'], val: [0.65, 9] } }, mounted() { this.showProgressBar(); }, created: function() { }, methods: { showProgressBar: function() { this.fitness.forEach((f, i) => { new ProgressBar.Circle('#container-' + i, { trailColor: 'gainsboro', trailWidth: 20, color: 'teal', strokeWidth: 20 }).animate(this.val[i]); }); } } }
 <div class="content" v-for="(f, index) in fitness"> <span>{{ f }}</span> <div v-bind:id="`container-${index}`"></div> </div>

Update - val can be an array.更新- val 可以是一个数组。 And its values can be referenced from the within the showProgressBar function.并且可以从showProgressBar函数中引用它的值。

I am assuming the length of fitness and val arrays are the same.我假设fitnessval数组的长度是相同的。

Update 2 - Explanation.更新 2 - 解释。

So basically there are 2 main issues here that have to addressed.所以基本上这里有两个主要问题需要解决。

1. Generating multiple container divs 1.生成多个container div

We need to generate multiple container divs as there are going to be multiple ProgressBars .我们需要生成多个container div,因为会有多个ProgressBars But we need to distinguish between them so we create a unique id for each of them.但是我们需要区分它们,所以我们为它们中的每一个创建一个唯一的 id。 That is what the following code does.这就是以下代码的作用。

<div class="content" v-for="(f, index) in fitness">
  <span>{{ f }}</span>
  <div v-bind:id="`container-${index}`"></div>
</div>

Since index numbers are unique.因为索引号是唯一的。 Adding them to "container" generated unique ids.将它们添加到“容器”会生成唯一 ID。 See List Rendering请参阅列表渲染

2. Generate multiple ProgressBars when component mounts. 2. 组件挂载时生成多个ProgressBars

This is simpler we simple create new ProgressBar for each "fitness" value.这更简单,我们只需为每个“适合度”值创建新的ProgressBar

this.fitness.forEach((f, i) => {
  new ProgressBar.Circle('#container-' + i, {
    trailColor: 'gainsboro',
    trailWidth: 20,
    color: 'teal',
    strokeWidth: 20
  }).animate(this.val[i]);

Refer - Array forEach参考 - 数组 forEach

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

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