javascript - 找不到模块 : Can't resolve 'styled-component'

标签 javascript reactjs styled-components

<分区>

我使用 create-react-app 创建了一个新的 React 应用程序,然后使用 npm 安装了 styled-component。但是当我在组件中使用它时,出现以下错误。

Failed to compile. ./src/Components/Lightbox/styledLightbox.js

Module not found:

Can't resolve 'styled-component' in '/Users/ishan/Documents/react-app/src/Components/Lightbox'

以下是组件:

import React, { Component } from 'react';
import { LightboxWrapper } from './styledLightbox';

class Lightbox extends Component {

    renderEntry() {
        if (this.props.lightbox.type === "photo") {
         return  <img alt="name" src={this.props.lightbox.entry} />;
        } 
        if (this.props.lightbox.type === "video") {
        return <video src={this.props.lightbox.entry} onLoadedMetadata={(e) => {console.log("Video duration is: "+e.currentTarget.duration) } } autoPlay muted onEnded={() => console.log("Video ended")} width='100%' height='100%' ></video> 
        }
        if(this.props.lightbox.type === "text"){
        return <div> <p className="lightbox text"> {this.props.lightbox.entry} </p> </div>   
        }

    }

    render() {
        let classList = this.props.lightbox.status === true ? "lightbox-wrapper active" : "lightbox-wrapper";
        return ( 
                <LightboxWrapper className={classList}>
                    {/* {this.renderEntry()} */}
                </LightboxWrapper>
         );
    }
}

export default Lightbox;

以下是为在其他组件中使用而创建的样式组件:

import styled from "styled-component";

export const LightboxWrapper = styled.div`
      display: none;
        position: fixed;
        z-index: 100;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
        overflow: auto;
        background: radial-gradient(
                center, 
                ellipse farthest-corner, 
                rgba(255,255,255,0) 0%,
                rgba(255,255,255,0) 100%
        );
    .active {
        display: block;
        background: radial-gradient(
            center, 
            ellipse farthest-corner, 
            rgba(255,255,255,0.5) 0%,
            rgba(255,255,255,0.1) 100%
    );
    }
    .active img, .active video {
        max-width: 70%;
        height: auto !important;
        margin: 0 auto;
        opacity: 1;
        /* box-shadow: 0px 2px 7px rgba(0,0,0,0.2); */
        transition: opacity 0.5s linear;
        animation: fadeInScale 1.2s ease-in-out;
        max-height: 70%;
        width: auto !important;
        position: relative;
        top: 11%;
}
`;

最佳答案

styled-components 末尾缺少“s”

$ npm i styled-components

关于javascript - 找不到模块 : Can't resolve 'styled-component' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54961117/

相关文章:

javascript - Ionic Framework - 尝试推送页面时未定义 'nav'

reactjs - 如何正确删除 React.js 中的事件监听器?

css - React 中的单选按钮选择

javascript - css flex 和输入框的问题

javascript - React Native 中如何区分每个用户?

javascript - 我想仅通过一个扬声器播放 : Play . mp3

javascript - 显示 image1 的脚本,然后(随机毫秒)显示 image2 并停止

javascript - Material UI 自动完成全宽

javascript - 我可以避免在路由更改时重新渲染父路由处理程序组件吗?

reactjs - 设置导入的样式组件的样式