代码之家  ›  专栏  ›  技术社区  ›  Anna K

将箭头函数移动到另一个导出函数

  •  0
  • Anna K  · 技术社区  · 6 年前

    我有问题。我想清除我的代码并将函数放到另一个文件中,但我总是得到一个错误:

    getMe is not a function
    

    为什么?我想在已经导出的函数getExchangeRateIntent中使用它。这会引起问题吗?

    JS

    const getRate = (base) => {
      console.log('My base currency is '+base);
    };
    
    module.exports = {getRate};
    

    GeTrATE.JS

    const getMe = ('./outside.js');
    
    module.exports = {
    
      'getExchangeRateIntent': (conv, parameter) => {
        const currencyBase = (parameter['currencyBase']);
        const currencyTarget = (parameter['currencyTarget']);
        const amount = (parameter['amount']);
        console.log(currencyBase);
        console.log(currencyTarget);
        console.log(amount);
    
        getMe('USD');
    
        conv.ask('nothing');
      },
    

    };

    1 回复  |  直到 6 年前
        1
  •  1
  •   Ivan Vasiljevic    6 年前

    module.exports = {getRate}; 您正在导出对象。使用您的导入:

    const getMe = ('./outside.js');

    您正在导入一个对象。所以这不是一个函数。这也不是正确的导入。

    为了正确导入,您可以这样写:

    import {getRate} from './outside.js ;

    像这样使用:

    getRate('USD');

    或者如果要使用,需要:

    const getMe = require('./outside.js');

    在第二种情况下,您可以这样调用函数:

    getMe.getRate('USD')