简体   繁体   English

我正在尝试将国家/地区列表放在 a.js 文件中并将其导出以供重复使用。 得到错误

[英]I am trying to put a list of countries in a .js file and export it for reuse. Getting errors

I am building a react/material-ui site and want to create a countries.js file that has the following in it (as an example)我正在构建一个 react/material-ui 站点,并希望创建一个包含以下内容的 countries.js 文件(例如)

  export default const countryList =  [
  {
    "label": "United States",
    "value": "US"
  },
  {
    "label": "Afghanistan",
    "value": "AF"
  },
   ...
  ]

I want to be able to import this as follows:我希望能够按如下方式导入它:

import countryList from '/json/countries.js'

where countryList will be the array that I can map like countryList.map...其中 countryList 将是我可以像 countryList.map 一样的 map 的数组...

I keep getting a parsing error, no matter how I define the function.无论我如何定义 function,我都会遇到解析错误。 What am I doing wrong?我究竟做错了什么?

UPDATE: After an answer from Prachi below, I tried to export/import like this:更新:在下面 Prachi 的回答之后,我尝试像这样导出/导入:

export const Countries = [
    {
      "label": "United States",
      "value": "US"
    },
    {
      "label": "Afghanistan",
      "value": "AF"
    }
]
export default Countries;

Then I import the file as follows.然后我按如下方式导入文件。 My intellisense tells me I have the correct path because I can see 'json' folder name in my intellisense list when typing:我的智能提示告诉我我有正确的路径,因为我可以在输入时在智能提示列表中看到“json”文件夹名称:

import Countries from '../../../json/Countries';

I still receive the following error:我仍然收到以下错误:

Module not found: Can't resolve '../../../json/Countries' in 'C:\projects\DoxaProject\DoxaUI\DoxaUI\ClientApp\src\pages\patientpage\forms'

Any help on solving this would be greatly appreciated!任何有关解决此问题的帮助将不胜感激!

The default export can only take an expression; default导出只能带表达式; const and let statements are not allowed.不允许使用constlet语句。 For example:- const MyComponent = () => { return <div class="foo">HI</div> ;例如:- const MyComponent = () => { return <div class="foo">HI</div> ; }; }; export default MyComponent;导出默认 MyComponent; You should write your code as:您应该将代码编写为:

        export const countryList = [
      {
        label: "United States",
        value: "US"
      },
      {
        label: "Afghanistan",
        value: "AF"
      }
    ];

export default countryList;

And import it as:并将其导入为:

   import countryList from "./country";

暂无
暂无

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

相关问题 为什么在尝试将转译的 js 文件导入我的应用程序时会收到 $ isotafunction 和 window.renderDashboardisnotafunction 的错误? - Why am I getting errors of $ isnotafunction and window.renderDashboardisnotafunction when trying to import a transpiled js file into my application? 我试图动态拉 <h3> xhtml文件中的元素并将其放入选择列表 - I am trying to dynamically pull <h3> elements from xhtml file and put them in a selection list 我正在尝试安装 react-native 但不断出现错误 - I am trying to install react-native but keep getting errors 当我尝试在 csv 中导出超过 1000 条记录时,出现页面无响应错误? - I am getting page unresponsive error when I am trying to export more than 1000 records in csv? 我正在尝试动态拉动所有 <h3> xhtml文件中的元素,并使用javascript将其放在选择列表中 - I am trying to dynamically pull all <h3> elements from xhtml file and put them in a selection list using javascript 尝试在 React Native 中将 mp3 文件作为参数传递时出现错误。 我该如何解决这个问题? 我正在使用 Expo-av 作为参考 - Trying to pass an mp3 file as a parameter in React Native and I am getting errors. How can I resolve this problem? I am using Expo-av for reference 我收到2美元的未定义错误 - I am getting 2 $ in not defined errors 我正在尝试将值从数据库传递给 js。 但是我收到了一个步进错误? - I am trying to pass values from Database to js. But i am getting an error of stepup? 我正在尝试下载React js,但是又一次又一次出现此错误? - I am trying to download React js, but I am getting this error again and again? 在js中的原型继承期间,当我尝试访问继承对象的方法时,我变得未定义 - during prototypal inheritance in js, i am getting undefined when i am trying to access the method of inherited object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM