我有一个货币列表:
ETH: 0,
BTC: 0,
USD: 0,
UAH: 0,
EUR: 0,
完整代码:
data() {
return {
selected: 'USD',
selected2: '',
tickers: [
{
id: 1,
name: 'USD'
},
{
id: 2,
name: 'EUR'
},
{
id: 3,
name: 'UAH'
},
],
ETH: 0,
BTC: 0,
USD: 0,
UAH: 0,
EUR: 0,
result: null,
value: 1,
modalOpened: false,
tickersNew: null,
currency: null,
}
获取这些货币的值:
async onChange() {
const [result, ETH, BTC, USD, EUR, UAH] = await Promise.all([
this.getValue(),
this.getValue('ETH'),
this.getValue('BTC'),
this.getValue('USD'),
this.getValue('EUR'),
this.getValue('UAH'),
])
this.result = result;
this.ETH = ETH;
this.BTC = BTC;
this.USD = USD;
this.EUR = EUR;
this.UAH = UAH;
},
async getValue(selectedTo = this.selected2) {
const API_KEY = 'API KEY HERE';
const loadTicker = () =>
fetch(
`https://min-api.cryptocompare.com/data/price?fsym=${this.selected}&tsyms=${selectedTo}&api_key=${API_KEY}`
).then(r => r.json()).then(r => r[selectedTo]);
return (await loadTicker())
},
我怎样才能使货币价值 be in a array-['ETH ',' BTC','USD',' EUR','UAH']这样我就可以向数组中添加数据,并得到数组中的结果。谢谢。
2条答案
按热度按时间bprjcwpo1#
这样,您就可以在coins对象中添加更多的股票代码,onChange将处理它们
aoyhnmkz2#
//一个示例(在任何框架之外)