请问老师为啥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
012020-09-06
相似问题