简体   繁体   English

vue 道具资料....能带一份吗?

[英]vue props data....Can you bring one?

I want to give different colors depending on the % of the 5 batteries.我想根据 5 节电池的百分比给出不同的颜色。 The problem is that the color isn't per-battery, it's spread across the entire battery.问题是颜色不是每个电池的颜色,它分布在整个电池中。

It's probably because bmss data comes in at once.可能是因为bmss数据一下子进来了。 But I don't know how to solve this.但我不知道如何解决这个问题。 It is implemented until the battery changes according to the % in green.一直执行到电池根据绿色百分比变化。 all battery image所有电池图像

The problem is color.问题是颜色。 color battery image彩色电池图像

https://codesandbox.io/s/battery-lx3dqz?file=/src/App.vue https://codesandbox.io/s/battery-lx3dqz?file=/src/App.vue

data vue数据视图

  <template>
      <div id="charging_div" class="frame">
        <div>
          <div id="charging_top_div">
            <div v-for="(bms, index) in bmss" :key="index">
              <div id="charging_left_div">
                <ProgressBattery :bmss="bms" />
                <span>{{ bmss[index].soc }}</span>
              </div>
            </div>
          </div>
          <div class="accumulate">
            <p>{{ time }} kW</p>
            <p>{{ power }} kW</p>
          </div>
          <div id="charging_bottom_div">
            <button id="harging_btn" v-on:click="click_stopCharging_btn">
              stop charging
            </button>
          </div>
        </div>
      </div>
    </template>
    
    <script>
    import ProgressBattery from "./ProgressBattery.vue";
    
    export default {
      name: "connect",
    
      components: {
        ProgressBattery,
      },
    
      data() {
        return {
          time: "",
          power: "",
          bmss: [
            {
              soc: 5,
            },
            {
              soc: 45,
            },
            {
              soc: 20,
            },
            {
              soc: 100,
            },
            {
              soc: 30,
            },
          ],
        };
      },

fetching vue获取 vue

 <template>
      <div id="progress-bar-battery">
        <div class="box">
          <span v-if="this.bmss.soc >= 100" class="item" id="v100"></span>
          <span v-if="this.bmss.soc >= 90" class="item" id="v90"></span>
          <span v-if="this.bmss.soc >= 80" class="item" id="v80"></span>
          <span v-if="this.bmss.soc >= 70" class="item" id="v70"></span>
          <span v-if="this.bmss.soc >= 60" class="item" id="v60"></span>
          <span v-if="this.bmss.soc >= 50" class="item" id="v50"></span>
          <span v-if="this.bmss.soc >= 40" class="item" id="v40"></span>
          <span v-if="this.bmss.soc >= 30" class="item" id="v30"></span>
          <span v-if="this.bmss.soc >= 20" class="item" id="v20"></span>
          <span v-if="this.bmss.soc >= 10" class="item" id="v10"></span>
        </div>
      </div>
    </template>
    
    <script>
    export default {
      name: "ProgressBattery",
      props: {
        bmss: {
          type: Object,
          required: true,
          default: function () {
            return [{ soc: 0 }];
          },
        },
      },
      mounted() {
        if (this.bmss.soc >= 61) {
          for (let i = 1; i < 11; i++) {
            document.getElementById(`v${i}0`).style.backgroundColor = "#4ee533";
            console.log(this.bmss.soc + "초록" + `v${i}0`);
          }
        } else if (this.bmss.soc >= 21 && this.bmss.soc <= 60) {
          for (let i = 1; i < 7; i++) {
            document.getElementById(`v${i}0`).style.background = "#FFA500";
            console.log(this.bmss.soc + "주황" + `v${i}0`);
          }
        } else if (this.bmss.soc <= 20 && this.bmss.soc >= 0) {
          for (let i = 1; i < 3; i++) {
            document.getElementById(`v${i}0`).style.background = "#FF0000";
            console.log(this.bmss.soc + "빨강" + `v${i}0`);
          }
        }
        console.log(this.bmss.soc);
      },
    };
    </script>

I think the issue in your approach that you use id html attribute for the style, and because there will be more than one battery so the id will not be unique.我认为您的方法中的问题是您使用 id html 属性作为样式,并且因为会有不止一个电池,所以 id 不会是唯一的。

It would better to use Style directive instead of changing style by id.最好使用 Style 指令而不是通过 id 更改样式。

I just fixed the issue and make some refractory in the code.我刚刚解决了这个问题并在代码中做了一些难处理的事情。

https://codesandbox.io/s/battery-forked-1ze0jx https://codesandbox.io/s/battery-forked-1ze0jx

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

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