【无标题】


```javascript

<template>
	<div class="view-div">
		<h2>货币汇率换算</h2><br />
		<table class="contain-div">
			<tr>
				<td><input type="text" v-model="inputJPY"></td>
				<td>日元JPY</td><br />
			</tr>
			<tr>
				<td><span>人民币:{{ receCNY }}</span></td><br/>
			</tr>
			<tr>
				<td><input type="text" v-model="inputCNY"></td>
				<td>人民币CNY</td><br/>
			</tr>
			<tr>
				<td><span>日元:{{ receJPY }}</span></td><br/>
			</tr>
		</table>
		<br />
	</div>
</template>
<script>
const API_KEY = "9e486aaedc2f40f5b8b8bee5aa55fd0b";
const API_URL = `https://openexchangerates.org/api/latest.json?app_id=${API_KEY}`;

export default {
	name: 'CurrencyListTwo',
	data() {
		return {
			inputJPY: 100,
			inputCNY: 100,
			receJPY: 0,
			receCNY: 0,
		}
	},

	methods: {

		async convertCurrency(amount, fromCurrency, toCurrency) {
			const response = await fetch(API_URL);
			const exchangeRates = await response.json();
			const convertedAmount = amount * exchangeRates.rates[toCurrency] / exchangeRates.rates[fromCurrency];
			return convertedAmount;
		}

	},
	watch: {
		inputJPY: {
			handler() {
				this.convertCurrency(
					this.inputJPY,
					"JPY",
					"CNY"
				).then((result) => { this.receCNY = result.toFixed(2) })
			},
			immediate: true
		},

		inputCNY: {
			handler() {
				this.convertCurrency(
					this.inputCNY,
					"CNY",
					"JPY"
				).then((result) => { this.receJPY = result.toFixed(2) })
			},
			immediate: true
		},
	}
}

</script>
<style scoped>
.iew-div {

	margin: auto;
	padding: 20px 20px;
	text-align: center;

}

.contain-div {

	margin: auto;


}
</style>

## 用watch写一个实时汇率换算的小功能(基于vue2)
主要考察watch监视属性

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值