RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1593950
Accepted
Danila Maslov
Danila Maslov
Asked:2024-09-16 06:55:36 +0000 UTC2024-09-16 06:55:36 +0000 UTC 2024-09-16 06:55:36 +0000 UTC

将 css 导入 React 应用程序时出错。我有一个模块,但我需要一个对象

  • 772

使用 typescript 测试 React 应用程序时出现错误。几个小时后,我意识到错误在于样式的导入方式。我花了几个小时在 gpt 聊天上,但没有多大帮助。当我像这样导入类时: import * as cls from './Button.module.scss'; concole.log(cls)返回Module {__esModule: true, Symbol(Symbol.toString Tag): 'Module'} 应用程序中没有错误,但与类相关的测试(玩笑)中有错误。

当我使用时: import cls from './Button.module.scss'; 应用程序中弹出错误:

TypeError: Cannot read properties of undefined (reading 'Button')
    at Button (http://localhost:3000/main.28f6bcc41182abdc5bde.js:5916:153)
    at renderWithHooks (http://localhost:3000/main.28f6bcc41182abdc5bde.js:23669:18)
    at mountIndeterminateComponent (http://localhost:3000/main.28f6bcc41182abdc5bde.js:28281:13)
    at beginWork (http://localhost:3000/main.28f6bcc41182abdc5bde.js:29804:16)
    at HTMLUnknownElement.callCallback (http://localhost:3000/main.28f6bcc41182abdc5bde.js:12347:14)
    at Object.invokeGuardedCallbackDev (http://localhost:3000/main.28f6bcc41182abdc5bde.js:12396:16)
    at invokeGuardedCallback (http://localhost:3000/main.28f6bcc41182abdc5bde.js:12460:31)
    at beginWork$1 (http://localhost:3000/main.28f6bcc41182abdc5bde.js:35668:7)
    at performUnitOfWork (http://localhost:3000/main.28f6bcc41182abdc5bde.js:34774:12)
    at workLoopConcurrent (http://localhost:3000/main.28f6bcc41182abdc5bde.js:34760:5)

console.log(cls)返回 Object,但通过此导入,测试中没有错误。

webpack.config.js:

const cssLoader = {
    test: /\.s[ac]ss$/i,
    use: [
        // Creates style nodes from JS strings
        isDev ? 'style-loader' : MiniCssExtractPlugin.loader,
        {
            loader: "css-loader",
            options: {
                modules: {
                    auto: (resPath: string) => Boolean(resPath.includes('.module.')),
                    localIdentName: isDev
                        ? '[path][name]__[local]--[hash:base64:8]'
                        : '[hash:base64:8]'
                }
            }
        },
        "sass-loader",
    ],
}

我没有对此文件进行任何更改。

全局.d.ts:

declare module '*.scss' { 
    interface IClassNames {
        [className: string]: string
    }
    const classNames: IClassNames;
    export = classNames;
}

这里我尝试用类型替换界面,但实际上设计本身保持不变。

笑话.config.ts:

moduleNameMapper: {
    '\\.(s?css)$': 'identity-obj-proxy',
},

在这里我还使用了不同的选项来指定文件扩展名,尽管这些更改没有意义

babel.config.json:

{
    "presets": [
        "@babel/preset-env",
        "@babel/preset-typescript",
        [
            "@babel/preset-react",
            {
                "runtime": "automatic",
            }
        ]
    ],
    "plugins": [
        "i18next-extract"
    ]
}

我没怎么接触过巴贝尔。

我添加到 Button.tsx 问题 :

import { classNames } from 'shared/lib/classNames/classNames';
import cls from './Button.module.scss';
import { ButtonHTMLAttributes, FC } from 'react';

export enum ThemeButton {
    CLEAR = 'clear',
    PRIMARY = 'primary'
}

interface ButtonProps extends ButtonHTMLAttributes<HTMLButtonElement> {
    className?: string;
    theme?: ThemeButton;
}

export const Button: FC<ButtonProps> = (props) => {
    const {
        className,
        children,
        theme,
        ...otherProps
    } = props;

    return (
        <button
            type='button'
            className={classNames(cls.Button, {}, [className, cls[theme]])}
            {...otherProps}
        >
            {children}
        </button>
    )
}

我添加到问题 2 Button.module.scss:

.Button {
    cursor: pointer;
}

.clear {
    padding: 0;
    margin: 0;
    border: none;
    background: none;
    outline: none;
}

告诉我如何消除错误并切换到import cls?

reactjs
  • 1 1 个回答
  • 50 Views

1 个回答

  • Voted
  1. Best Answer
    Danila Maslov
    2024-09-17T06:39:08Z2024-09-17T06:39:08Z

    问题出在 css-loader 版本上。版本 7.1.2 引发错误。安装版本 6.6.0 后,错误消失。

    • 0

相关问题

  • 如果图像在道具中,如何使背景图像做出反应?

  • 项目未显示

  • 引发错误:InvalidTokenError: Invalid token specified: Cannot read property 'replace' of undefined

  • 如何在没有 node.js 的情况下运行 react.js 应用程序

  • 如何从 React Native 中的导航堆栈中清除上一个屏幕?

  • 为什么渲染后会触发 Click 事件?

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5