简体   繁体   English

如何在 JavaScript 中将导入的 function 调用到另一个 function 中?

[英]How to call an imported function into another function in JavaScript?

I have a function called getConversionRate() I created in a separate file:我有一个名为 getConversionRate() 的 function 我在一个单独的文件中创建:

export function getConversionRate() {
  let usaRate = 0.74;
  if (isEnvironmentBFE()) {
    const catName = "CATERPILLAR FINANCIAL SERVICES CORPORATION";

    // If it contains Cat Financial products, make US rate 1.32.
    if (detailsHtml.dealerId === catName) {
      usaRate = 1.32;
    }
  }
}

I've imported getConversionRate() into another file, and I want to import that function inside of another function:我已将 getConversionRate() 导入另一个文件,我想将 function 导入另一个 function 中:

//PRICE
  if (
    props.jsonDataProduct.price != null &&
    props.jsonDataProduct.price.text != null
  ) {
    detailsHtml.itemPriceCA = formatPrice(
      props.jsonDataProduct.price.text,
      props.lang
    );
    detailsHtml.itemPriceUS = formatPrice(
      props.jsonDataProduct.price.text,
      props.lang,
      false,
      "US"
    );
  }

Here is the original formatPrice() function:这是原始格式Price() function:

export function formatPrice(
  price,
  lang,
  inclCurTxt?: boolean,
  currency?: string
) {
  let formattedPrice = price;

  const usaRate = 0.74;

  if (lang === "fr") {
    //FRENCH
    const currencyText = inclCurTxt ? " CA" : "";
    if (currency != null && currency === "US") {
      //USD
      formattedPrice =
        accounting.formatMoney(Number(price) * usaRate, "", 0, " ") +
        " $" +
        currencyText;
    } else {
      //CAD
      formattedPrice =
        accounting.formatMoney(price, "", 0, " ") + " $" + currencyText;
    }
  } else {
    //ENGLISH
    const currencyText = inclCurTxt ? " USD" : "";
    if (currency != null && currency === "US") {
      //USD
      formattedPrice =
        accounting.formatMoney(Number(price) * usaRate, "$", 0) + currencyText;
    } else {
      //CAD
      formattedPrice = accounting.formatMoney(price, "$", 0) + currencyText;
    }
  }
  return formattedPrice;
}

And both itemPriceCA and itemPriceUS would be rendered on a page in React, reflecting the correct price based on getConversionRate(): itemPriceCA 和 itemPriceUS 都将在 React 的页面上呈现,反映基于 getConversionRate() 的正确价格:

<div className="price-wrap">
   <img
    className="flagimg"
    src={Constants.IMAGES.FLAG_CA}
    alt="CDN $"
     />
    <span className="data_price_cad">{detailsHtml.itemPriceCA}</span>
 </div>

<div className="price-wrap">
   <img
    className="flagimg"
    src={Constants.IMAGES.FLAG_US}
    alt="US $"
    />
    <span className="data_price_usd">{detailsHtml.itemPriceUS}</span>
</div>

How do I go about inserting getConversionRate() into formatPrice() succesfully?我如何 go 成功地将 getConversionRate() 插入 formatPrice()

Just import getConversionRate in the same file where you are declaring and using formatPrice .只需在您声明和使用formatPrice的同一文件中导入getConversionRate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM