我正在使用API来获取货币汇率。基地&输出货币由用户输入。
这是我的后端:
import {fetch} from 'wix-fetch';
export function getCurrency(currency,baseCurrency) {
const url = 'https://api.fixer.io/latest?base=';
let fullUrl = url + baseCurrency + '&symbols=' + currency;
return fetch(fullUrl, {method: 'get'})
.then(response => response.json())
.then(json => json.rates[currency].toString());
}
这是我的网页代码:
import {getCurrentTemp, getCurrency} from 'backend/ArenaApi';
export function button1_click(event, $w) {
getCurrency($w("#currencysymbol").value,$w("#baseCurrency").value)
.then(rates => $w("#currencyrate").text = "1 " + ($w("#baseCurrency").value) + " = " + rates + " " + ($w("#currencysymbol").value));
$w("#currencyrate").show();
}
根据我的理解,我的后端正在获取这些数据:
"base":"USD","date":"2018-05-22","rates":{"EUR":0.84789}
它正在通过后端的以下行从此字符串中检索“rate”:
.then(json => json.rates[currency].toString());
我想知道如何从单个字符串中检索多个值?对于此实例:“费率” “基本” “日期”,然后将其注入页面中的文本元素,如:
.then(rates => $w("#currencyrate").text = "1 " + ($w("#baseCurrency").value) + " = " + rates + " " + ($w("#currencysymbol").value));