[英]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.不允许使用const
和let
语句。 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.