请问老师为啥classnames会找不到呢,我已经安装了,重新 npm install也没有用

来源:4-8 小试牛刀 - Button 组件编码 第一部分

再见地平线

2020-09-06

图片描述

import React from 'react';
import classNames from 'classnames';

export enum ButtonSize {
    Large = 'lg',
    Small = 'sm',
}

export enum ButtonType {
    Primary = 'primary',
    Danger = 'danger',
    Link = 'link',
    Default = 'default',
}

interface ButtonProps {
    size?: ButtonSize;
    btnType?: ButtonType;
    className?: string;
    disabled?: boolean;
    children: React.ReactNode;
    href?: string;
}

const Button = (props: ButtonProps) => {
    const {
        size,
        btnType,
        disabled,
        children,
        href,
    } = props;

    const classes = classNames('fun-btn', {
        [`fun-btn-${btnType}`]: btnType,
        [`fun-btn-${size}`]: size,
        'fun-btn-disabled': (btnType === ButtonType.Link) && disabled
    })

    if(btnType === ButtonType.Link && href) {
        return (
            <a 
                className={classes}
                href={href}
            >
                {children}
            </a>
        )
    } else {
        return (
        <button 
            className={classes}
            disabled={disabled}
        >
            {children}
        </button>
        )
    }
}

Button.defaultProps = {
    disabled: false,
    btnType: ButtonType.Default
}

export default Button;
写回答

1回答

张轩

2020-09-06

同学你好 请问你安装了 类型依赖了嘛? classNames 是需要单独的 type 文件的。

npm install --save @types/classnames


0
1
再见地平线
谢谢老师,已解决
2020-09-06
共1条回复

React18+TS高仿AntD从零到一打造组件库

设计,开发,测试,发布再到 CI/CD,从0到1造轮子

2123 学习 · 959 问题

查看课程