简体   繁体   English

如何使用 Javascript 在 React 中使用“从 src/* 导入组件”导入所有组件,styles 等?

[英]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-resolverBabel插件。 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.

相关问题 如何从React组件中的src文件夹导入JavaScript? - How to import a javascript from src folder in a react component? 有什么方法可以将 JavaScript 函数从 src 文件夹导入到 React 组件之外 - Is there any way to import JavaScript function to React component outside from src folder 如何在React中导入特定于组件的CSS样式? - How can I import component-specific css styles in React? 如何在反应中从主捆绑文件中拆分延迟加载的子组件的所有导入语句(使用 webpack 4) - How to split all import statements of a lazy loaded child component from the main bundle file in react (using webpack 4) 如何将javascript从组件导入另一个组件? - How to import javascript from component to another component? 如何从第二个 Javascript 文件中获取标签元素(标题、src 等) - How to get Tag Elements (Title, src etc.) from a second Javascript File 如何通过反应动态导入组件? / 动态导入 - how to dynamically import components with react ? / Dynamic import 如何在反应中从不同的驱动器导入组件? - How to import a component from a different drive in react? 如何从github导入React组件? - How import a React component from github? 当使用样式组件时,有必要`&#39;从&#39;react&#39;中导入React? - Necessary to `import React from 'react'` when using styled-components?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM