javascript - Vue.js 进度条

标签 javascript css twitter-bootstrap vue.js

我正在使用 vue.js 2.0 我有这个方法:

calculatePercentage(option) {
    let totalVotes = 0;

    this.poll.options.forEach((option) => {
        totalVotes+= option.votes.length;
    });

    return option.votes.length / totalVotes * 100;
}

这是我的 Bootstrap 进度条:

<div class="span6">
    <div v-for="option in poll.options">
        <strong>{{ option.name }}</strong><span class="pull-right">{{ calculatePercentage(option) }}%</span>
        <div class="progress progress-danger active" aria-valuenow="12">
            <div class="bar" style="width: 15%;"></div>
        </div>
    </div>
</div>

所以 calculatePercentage(option); 工作正常。但是我如何将它绑定(bind)到样式 (style="width: 15%;") ?

非常感谢

最佳答案

您可以按照说明将内联样式绑定(bind)到 vue 数据 here .您需要做的就是从 calculatePercentage 返回值并按如下方式使用它:

<div class="span6">
    <div v-for="option in poll.options">
        <strong>{{ option.name }}</strong><span class="pull-right">{{ calculatePercentage(option) }}%</span>
        <div class="progress progress-danger active" aria-valuenow="12">
            <div class="bar" v-bind:style="{width: calculatePercentage(option) + '%'}"></div>
        </div>
    </div>
</div>

关于javascript - Vue.js 进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42132900/

相关文章:

javascript - 无法在 onUpdatedWithMemory(chrome 扩展 API)中显示 CPU 和网络值

html - 菜单和内容与标题中的图像重叠

css - 我怎样才能让我的CSS :hover work?

html - bootstrap,我无法让容器变得流畅(全宽)col-md-12

javascript - 通过 JavaScript 设置 2 个 div 的高度

css - Bootstrap : two files with same code in same browser different results

javascript - 使用 each() 选择并计算未选中复选框的长度

javascript - 仅当单击两个按钮时如何操作元素

javascript - react : How to manage state for multiple input types

html - 将图像完美地贴合在页面上