简体   繁体   English

如何在ES6中访问和使用导入功能

[英]How to access and use Imported Function in ES6

I'm having trouble accessing the imported labelsForCountryCode function. 我在访问导入的labelsForCountryCode函数时遇到问题。 When passing 'US' into this function, I'm expecting to have the US object returned. 当将“ US”传递给此函数时,我期望返回US对象。 I'm new to ES6 and not exactly sure what I'm doing wrong here. 我是ES6的新手,不确定自己在做什么错。 Appreciate any help! 感谢任何帮助!

billing-address-labels-lookup.js billing-address-labels-lookup.js

export default function billingAddressLabelsLookup() {

const labelsLookup = {
  'US': {
    addressLineOne: 'Street/Address',
    city: 'City',
    provinceCode: 'State',
    postalCode: 'Zip Code'
  },
  'CA': {
    addressLineOne: 'Street/Address',
    city: 'City',
    provinceCode: 'Province',
    postalCode: 'Postal Code'
  }
};

return {
  labelsForCountryCode(countryCode) {
    return labelsLookup[countryCode];
  }
};

}; };

test.js test.js

 import billingAddressLabelsLookup from '../utils/billing-address-labels-lookup.js';

 var labels = billingAddressLabelsLookup.labelsForCountryCode('US');

Just replace: 只需替换:

var labels = billingAddressLabelsLookup.labelsForCountryCode('US');

with: 与:

var labels = billingAddressLabelsLookup().labelsForCountryCode('US');
                                       ^^------ function call

Otherwise, you're trying to access the function reference's property , while you need to access the function's result . 否则,您将尝试访问函数引用的属性 ,而您需要访问函数的result

About import and exports, please take a look at this chapter specifically: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export#Using_the_default_export 关于导入和导出,请具体看一下本章: https : //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/export#Using_the_default_export

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

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