[英]How to import all your components, styles, etc. using 'import component from src/*' in React with Javascript?
When I try one of the following two imports当我尝试以下两个导入之一时
import 'src/assets/styles/App.scss'
-
import 'assets/styles/App.scss'
I'm receiving the following error's respectively我分别收到以下错误
Module not found: Can't resolve 'src/assets/styles/App.scss' in '/path/to/my/project/src'
-
Module not found: Can't resolve 'assets/styles/App.scss' in '/path/to/my/project/src'
In typescript I believe that I was fixing this by having在 typescript 中,我相信我正在通过
"include": [
"./src/**/*"
]
in my tsconfig.json, either that or maybe在我的 tsconfig.json 中,或者可能
"compilerOptions": {
...
"baseUrl": "./",
...
}
I'm not sure completely sure though, because these options were created by create-react-app不过我不确定完全确定,因为这些选项是由 create-react-app 创建的
How would I create this effect in just plain javascript in React?我如何在 React 中仅用普通的 javascript 创建这种效果?
Update更新
I created a jsconfig.json file containing我创建了一个包含 jsconfig.json 的文件
{
"compilerOptions": {
"baseUrl": "./",
}
}
and receive the following error output并收到以下错误 output
internal/modules/cjs/loader.js:800 throw err; ^ SyntaxError: /path/to/my-app/jsconfig.json: Unexpected token } in JSON at position 62 at JSON.parse (<anonymous>) at Object.Module._extensions..json (internal/modules/cjs/loader.js:797:27) at Module.load (internal/modules/cjs/loader.js:653:32) at tryModuleLoad (internal/modules/cjs/loader.js:593:12) at Function.Module._load (internal/modules/cjs/loader.js:585:3) at Module.require (internal/modules/cjs/loader.js:692:17) at require (internal/modules/cjs/helpers.js:25:18) at getModules (/path/to/my-app//node_modules/react-scripts/config/modules.js:133:14) at Object.<anonymous> (/path/to/my-app/node_modules/react-scripts/config/modules.js:149:18) at Module._compile (internal/modules/cjs/loader.js:778:30) npm ERR. code ELIFECYCLE npm ERR. errno 1 npm ERR: my-app@0.1.0 start. `react-scripts start` npm ERR. Exit status 1 npm ERR. npm ERR: Failed at the my-app@0.1.0 start script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above. npm ERR! A complete log of this run can be found in: npm ERR! /home/me/.npm/_logs/2020-09-22T17_18_27_953Z-debug.log
In case you're using Webpack, edit your webpack.config.js
with:如果您使用的是 Webpack,请使用以下命令编辑您的
webpack.config.js
:
resolve: {
alias: {
assets: path.resolve(__dirname, 'src/assets/'),
components: path.resolve(__dirname, 'src/components/')
}
}
In case not, you could use a Babel plugin called babel-plugin-module-resolver .如果没有,您可以使用名为babel-plugin-module-resolver的Babel插件。 To activate this plugin functionality, create the file
.babelrc
, or edit in case you already have it with this code:要激活此插件功能,请创建文件
.babelrc
,或者如果您已经使用此代码进行编辑:
{
"plugins": [
["module-resolver", {
"root": ["./src"],
alias: {
assets: path.resolve(__dirname, 'src/assets/'),
components: path.resolve(__dirname, 'src/components/')
}
}]
]
}
This code will define the alias of the paths assets & components.此代码将定义路径资产和组件的别名。 Then, you can use them to import directly as per your example:
然后,您可以根据您的示例使用它们直接导入:
import * as assets from 'assets/myModule'
Try with creating a.env file that contains: NODE_PATH=src/尝试创建一个包含以下内容的 .env 文件:NODE_PATH=src/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.