Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Css 使用React-Typescript显示图像的按钮_Css_Reactjs_Typescript_Emotion - Fatal编程技术网

Css 使用React-Typescript显示图像的按钮

Css 使用React-Typescript显示图像的按钮,css,reactjs,typescript,emotion,Css,Reactjs,Typescript,Emotion,我正在使用React-Typescript 如何将图像添加到按钮? 我试着这样做,没有错误,但我得到了一个空白按钮 如何在这里使用情感CSS设计我的按钮元素 从“React”导入React; 功能按钮(道具){ 让样式={ 宽度:24, 身高:24, src:“../images/abc.png” }; 返回( props.handleClick()}>{props.label} ); } 返回( ); } src将是背景:url(../images/abc.png) 也许您混淆了srcprop

我正在使用React-Typescript

  • 如何将图像添加到按钮?
    我试着这样做,没有错误,但我得到了一个空白按钮
  • 如何在这里使用情感CSS设计我的按钮元素
  • 从“React”导入React;
    功能按钮(道具){
    让样式={
    宽度:24,
    身高:24,
    src:“../images/abc.png”
    };
    返回(
    props.handleClick()}>{props.label}
    );
    }
    返回(
    );
    }
    
    src
    将是
    背景:url(../images/abc.png)
    也许您混淆了
    src
    prop,而
    img
    HTML元素会有这个属性

    也许您应该在按钮中包含嵌套的
    img
    组件


    工作示例:

    Damian的答案在技术上是正确的,但是如果您使用的是webpack,则需要像这样导入图像:

    import abc from "../images/abc.png";
    
    function PostButton(props){
        let style = {
            width:24,
            height:24,
            background: `url(${abc})`,
        };
        return(
            <button style={style} onClick={() => props.handleClick()}>{props.label}</button>
        );
    }
    
    然后像这样使用它:

    import abc from "../images/abc.png";
    
    function PostButton(props){
        let style = {
            width:24,
            height:24,
            background: `url(${abc})`,
        };
        return(
            <button style={style} onClick={() => props.handleClick()}>{props.label}</button>
        );
    }
    
    功能按钮(道具){
    让样式={
    宽度:24,
    身高:24,
    背景:`url(${abc})`,
    };
    返回(
    props.handleClick()}>{props.label}
    );
    }