亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

VueJS:如何在 App.vue 中編輯所有相同類型的組件

VueJS:如何在 App.vue 中編輯所有相同類型的組件

一只斗牛犬 2022-10-13 09:47:06
如果我有一個這樣的模板,其中包含 FruitPrices 作為組件:<template>  <div id="app">    <div>      <span @click=SOME_FUNCTION> Change currency </span>      <FruitPrices fruit="apple"></FruitPrices>      <FruitPrices fruit="banana"></FruitPrices>      <FruitPrices fruit="orange"></FruitPrices>             ...             ....             .....</template>.........import FruitPrices from ./component/FruitPrices.vue我想知道是否有可能讓 SOME_FUNCTION 成為一種改變所有 FruitPrices 組件中貨幣的方法?即,如果某些組件是美元,有些是英鎊,我可以通過當前主應用程序中的方法將它們全部設置為歐元嗎?
查看完整描述

3 回答

?
慕虎7371278

TA貢獻1802條經驗 獲得超4個贊

您可以在數據對象中為貨幣設置一個屬性,并在所有組件中使用該屬性,并使用一個函數來更改它。


data() {

  return {

    currency: "USD",

    ...

  }

},

methods: {

  changeCurrency(newCurrency){

    this.currency = newCurrency;

  }

}

并在模板中


<span @click=changeCurrency('Euro')> Change currency </span>


查看完整回答
反對 回復 2022-10-13
?
手掌心

TA貢獻1942條經驗 獲得超3個贊

我不知道我是否明白了你的問題的邏輯,但也許你可以像這樣使用 vue道具:


      <FruitPrices fruit="apple" :currency="newCurrency || 'USD'"></FruitPrices>

      <FruitPrices fruit="banana" :currency="newCurrency || 'GBP'"></FruitPrices>

      <FruitPrices fruit="orange" :currency="newCurrency || 'USD'"></FruitPrices>

currency您可以在組件內定義道具FruitPrices。如果currency未定義 prop,則將第二種貨幣作為默認值(例如"newCurrency || 'GBP'",如果newCurrency為 null,則貨幣 GBP 將作為默認值)。


用于道具的 Vue 文檔:https ://v2.vuejs.org/v2/guide/components-props.html


然后在主模板組件中,定義變量newCurrency并將該變量傳遞給currency組件的 prop FruitPrices:


data: () => {

    newCurrency: null

},

methods: {

    setNewCurrency() {

        this.newCurrency = this.someFieldOfYourForm;

    }

}


查看完整回答
反對 回復 2022-10-13
?
MMTTMM

TA貢獻1869條經驗 獲得超4個贊

讓我們使用這個組件將貨幣、價格和水果傳遞給每個組件 FruitPrices 組件:


<template>

  <div>

    <!-- USD is selected by default, once the option change, the prices will be updated -->

    <select v-model="currency">

      <!-- allows to select one currency at time, it shows all the currencies

          that exist in the currencies array data property -->

      <option

        v-for="(currencyOption, index) in currencies"

        :key="index"

        :value="currencyOption"

      >

        <!-- show only the currency name as option -->

        {{ currencyOption.name }}

      </option>

    </select>

    <div v-for="(fruit, fruitIndex) in fruitsWithPrices" :key="fruitIndex">

      <!-- if everything is working fine, you don't need close tags for empty

          custom component, you can use '/' at the end of the first tag to self close it-->

      <FruitPrices :name="fruit.name" :convertedPrice="fruit.convertedPrice" />

    </div>

  </div>

</template>

<script>

import FruitPrices from '../somePlace/FruitPrices'

export default {

  name: "FruitPricesContainer",

  components: { FruitPrices },

  data: () => ({

    fruits: [

      {

        name: 'Apple',

        price: 0.2

      },

      {

        name: 'Banana',

        price: 0.3

      },

      {

        name: 'Orange',

        price: 0.25

      }

    ],

    currency: {

      // Base currency, exchangeRate = 1

      exchangeRate: 1,

      name: 'USD'

    },

    // Used exchange rates only for demo purpose, not are the real and valid exchange rates

    currencies: [

      {

        exchangeRate: 1,

        name: 'USD'

      },

      {

        exchangeRate: 1.2,

        name: 'EUR'

      },

      {

        exchangeRate: 0.7,

        name: 'SGD'

      },

      {

        exchangeRate: 700,

        name: 'MXN'

      },

      {

        exchangeRate: 3700,

        name: 'COP'

      }

    ]

  }),

  computed: {

    // The fruitsWithPrices listen for changes in both, the fruit and

    // the currency, so once you change it by selecting a different currency

    // the prices will be updated automatically (nice)

    fruitsWithPrices() {

      return this.fruits.map((fruit) => {

        return {

          ...fruit, // Add name and original price to the returned object

          convertedPrice: fruit.price * this.currency.exchangeRate // Add the price based on the exchange rate

        }

      })

    }

  }

}

</script>

現在,讓我們創建 FruitPrices 組件:


<template>

  <div>

    <p>{{ name }}: {{ convertedPrice }}</p>

  </div>

</template>

<script>

export default {

  name: "FruitPrices",

  props: {

    name: {

      type: String,

      required: true

    },

    convertedPrice: {

      type: Number,

      required: true

    }

  }

}

</script>

它已經準備好了?。═bh,我沒有測試它,如果您有錯誤或問題,請告訴我)。


查看完整回答
反對 回復 2022-10-13
  • 3 回答
  • 0 關注
  • 145 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號