繁体   English   中英

'SyntaxError: 不能在模块外使用 import 语句'

[英]'SyntaxError: Cannot use import statement outside a module'

在此处输入图像描述从样式组件导入 createGlobalStyle 时出现此问题。 我还尝试在 package.json 文件中添加“type”:“module”,但没有任何意义。

[全球风格.jsx]

    import { createGlobalStyle } from "styled-components";
// eslint-disable-next-line
import React from 'react';

const GlobalStyle = createGlobalStyle` 
    body {
        width: 100%;
        margin: 0;
    }
`; 

export default GlobalStyle; 

[包.json]

{
  "name": "dev-lio",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.16.4",
    "@testing-library/react": "^13.3.0",
    "@testing-library/user-event": "^13.5.0",
    "react": "^18.1.0",
    "react-dom": "^18.1.0",
    "react-router-dom": "^6.3.0",
    "react-scripts": "^5.0.1",
    "web-vitals": "^2.1.4"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "babel-plugin-styled-components": "^2.0.7"
  }
}

任何意见,将不胜感激。

[登录.jsx]

import React from 'react'
import GlobalStyle from '../../components/GlobalStyle';
import GlobalFonts from '../../fonts/fonts'
import LoginForm from '../../components/LoginForm';

const Login = () => {
    return (
        <div>
            <LoginForm />
            <GlobalFonts />
            <GlobalStyle />
        </div>
    )
}

export default Login;

[索引.jsx]

import React from 'react'
import GlobalStyle from '../../components/GlobalStyle';
import Header from '../../components/Header';
import Profile from '../../components/Profile';
import GlobalFonts from '../../fonts/fonts';

const Main = () => {
    return (
        <div>
            <Header />
            <Profile />
            <GlobalFonts />
            <GlobalStyle />
        </div>
    )
}

export default Main;

就是在上面两个文件中使用了 GlobalStyle。

import { createGlobalStyle } from "styled-components";

此导入标记意味着 createGlobalStyle 未默认导出。 如果您尝试将命名值导入为默认值,则会引发错误。

import createGlobalStyle from 'styled-components' 

将是默认导出的正确用法

您也可以尝试 module.export,但我从来不需要在反应中使用它。 大多数时候问题在于我如何实现默认/命名导入

暂无
暂无

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

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