简体   繁体   English

如何通过写一个function来分发图片?

[英]how to distribute pictures by writing one function?

I am working on exchange rates.我正在研究汇率。 I have a list of currencies.我有一份货币清单。 Each currency has its own picture, its own numbers.每种货币都有自己的形象,自己的数字。 And if these numbers have changed, then in this case the icon changes too, for example, there are three cases: if the currency has increased the up arrow, if the down arrow has gone down, if it has not changed, the dot icon.而如果这些数字发生了变化,那么在这种情况下,图标也会发生变化,例如,有三种情况:如果货币增加了向上箭头,如果向下箭头下降,如果没有变化,点图标.

Data comes from the backend.数据来自后端。 That is, I take all the data from via mapState and display it.也就是说,我通过 mapState 获取所有数据并显示它。 And then I go through the loop through v-for and deduce the elements.然后我 go 通过 v-for 循环并推导出元素。 So, I did all this as follows:所以,我做了所有这一切如下:

   <q-list>
        <div class="list-block" v-for="(item, index) in cashData.isNoCross" :key="index + '_exchangeRateList'">
          <q-item class="list-block__element">
            <q-item-section class="list-block__section">
             <img :src="`./statics/icons/currency-icons/${item.currency}.svg`" />
              <span class="indent-left">{{ item.currency }}</span>
            </q-item-section>
          </q-item>
 <q-item>
            <q-item-sectioт>
              <span class="title title--blue">{{ item.buyPrice }}</span>
              <img
                v-if="item.buyStatus != 'unchanged'"
                :src="`./statics/icons/currency-icons/arrow-${item.buyStatus === 'isUp' ? 'isUp' : 'isDown'}.svg`"
              />

              <img
                v-else
                :src="`./statics/icons/currency-icons/arrow-${
                  item.buyStatus === 'unchanged' ? 'unchanged' : 'unchanged'
                }.svg`"
              /> 
              <img
                src="../../statics/icons/currency-icons/arrow-isUp.svg"
              /> 
            </q-item-section>
          </q-item>
         </div>
         </q-list>

It all works great.这一切都很好。 But there is too much code, since I have to distribute the pictures in their places in other cases.但是代码太多了,因为在其他情况下我必须将图片分发到他们的位置。 Therefore, I was advised to write everything in one function, as here .因此,建议我将所有内容都写在一个 function 中, 如此处

So I did like this:所以我这样做了:

       <q-item>
            <q-item-section>
              <span class="title title--blue">{{ item.buyPrice }}</span>
              <div v-for="(pic, index) in item.buyStatus" :key="index">
                <img :src="getImgUrl(pic)" />
              </div>
            </q-item-section>
          </q-item>
         ... 
  methods: {
getImgUrl(pic) {

  if (pic === "isUp") {
    return require("../../statics/icons/currency-icons/arrow-" + pic + ".svg");
  } else if (pic === "isDown") {
    return require("../../statics/icons/currency-icons/arrow-" + pic + ".svg");
  } else if (pic === 'unchanged') {
    return require("../../statics/icons/currency-icons/arrow-" + pic + ".svg");
  } else {
    return require("../../statics/icons/currency-icons/" + pic + ".svg");
  }
}

In the last check, I want pictures to be inserted, namely the flags of the countries of the corresponding currency.在最后的检查中,我要插入图片,即相应货币的国家的国旗。

I seem to understand what needs to be done, but I can't understand at the same time.我似乎明白需要做什么,但我不能同时明白。 I would be grateful for any answers.我将不胜感激任何答案。

It looks like that "pic" variable (item.buyStatus) can be used in the image url string rather than applying conditions to it and then adding it to the string based on those conditions.看起来“pic”变量(item.buyStatus)可以在图像 url 字符串中使用,而不是对其应用条件,然后根据这些条件将其添加到字符串中。

Is this what you want?这是你想要的吗?

<q-list>
  <div class="list-block" v-for="(item, index) in cashData.isNoCross" :key="item.currency">
    <q-item class="list-block__element">
      <q-item-section class="list-block__section">
        <img :src="`./statics/icons/currency-icons/${item.currency}.svg`" />
        <span class="indent-left">{{ item.currency }}</span>
      </q-item-section>
    </q-item>
    <q-item>
      <q-item-section>
        <span class="title title--blue">{{ item.buyPrice }}</span>
        <img :src="require(`./statics/icons/currency-icons/arrow-${item.buyStatus}.svg`)"/>
      </q-item-section>
    </q-item>
  </div>
</q-list>

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

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