簡體   English   中英

我正在嘗試構建一個簡單的貨幣轉換器。 我的 function 轉換貨幣一直給我一個錯誤

[英]I'm trying to build a simple currency converter. my function to convert the currency keeps giving me an error

我收到此錯誤“算術運算的右側必須是“任何”、“數字”、“大整數”或枚舉類型。

import { Component, OnInit } from '@angular/core';
    
    @Component({
      selector: 'app-convert',
      templateUrl: './convert.component.html',
      styleUrls: ['./convert.component.css']
    })
    export class ConvertComponent implements OnInit {
      amount = 1;
      from = 'USD';
      to = 'GBP';
      rate = '1.37';
    
      // function for conversion.
      convert(): number {
        return this.amount * this.rate;
      }
    
        
    
      constructor() {}
    
      ngOnInit(): void {}
    }

任一變化rate = '1.37'; rate = 1.37;
或更改return this.amount * this.rate; return this.amount * Number(this.rate);

您還應該將變量from重命名為convertFrom ,因為from是保留術語。

你寫了

rate = ‚1.37‘

這被解釋為字符串。 寫成

rate=1.37 

然后它應該工作

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM