最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Passing dynamic styles to my element in VueJS - Stack Overflow

programmeradmin3浏览0评论

I am trying to make a type of progress bar to track a percentage of tasks pleted. I want to v-bind:styles and pass it {width: dynamicWidth + '%'} in order to control the progression of this bar. So far I have created a puted variable that will return the percentage plete I want to bar to display, and I have set up my dynamic style in the data object

  export default{
    data: function () {
      return {
        numQuotes: dataBus.numQuotes,
        numberA: 30,
        barWidth: {
          width: this.barWidthCalculated +'%'
        }
      }
    },
    puted: {
      barWidthCalculated: function(){
        return this.numQuotes * 10;
      }
    }
  }

I also added an element to the DOM to see what was happening.

  <div id="trackerBar">
        <div id="trackerBarActual" v-bind:style="barWidth">
            <h2>{{numQuotes}}/10</h2>
            <p>{{barWidthCalculated}}</p>
        </div>
    </div>

My bar stays fixed at 100%, i dont see any interpolation on the DOM. I also established another NUMBER variable in my data section and attempted to pass that to my width property, but still no change, and no rendering on the DOM. However if I pass any other elements in my styles object such as

color: 'red'

Those changes take place. Also if I pass my styles object a number directly ie...

  barWidth: {
        width: 50 +'%'
  }

It displays correctly on the DOM.

What am I missing/doing wrong?

I am trying to make a type of progress bar to track a percentage of tasks pleted. I want to v-bind:styles and pass it {width: dynamicWidth + '%'} in order to control the progression of this bar. So far I have created a puted variable that will return the percentage plete I want to bar to display, and I have set up my dynamic style in the data object

  export default{
    data: function () {
      return {
        numQuotes: dataBus.numQuotes,
        numberA: 30,
        barWidth: {
          width: this.barWidthCalculated +'%'
        }
      }
    },
    puted: {
      barWidthCalculated: function(){
        return this.numQuotes * 10;
      }
    }
  }

I also added an element to the DOM to see what was happening.

  <div id="trackerBar">
        <div id="trackerBarActual" v-bind:style="barWidth">
            <h2>{{numQuotes}}/10</h2>
            <p>{{barWidthCalculated}}</p>
        </div>
    </div>

My bar stays fixed at 100%, i dont see any interpolation on the DOM. I also established another NUMBER variable in my data section and attempted to pass that to my width property, but still no change, and no rendering on the DOM. However if I pass any other elements in my styles object such as

color: 'red'

Those changes take place. Also if I pass my styles object a number directly ie...

  barWidth: {
        width: 50 +'%'
  }

It displays correctly on the DOM.

What am I missing/doing wrong?

Share asked Jul 28, 2017 at 4:39 DMrFrostDMrFrost 9112 gold badges16 silver badges34 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

why not just use :

  <div id="trackerBarActual" v-bind:style="barWidthCalculated">

  puted: {
    barWidthCalculated: function(){
      return {
        width: (this.numQuotes * 10) + '%', 
        color: 'red' 
      };
    }
发布评论

评论列表(0)

  1. 暂无评论