【问题标题】:Typescript import a React js component which d.ts file writed by myself and occured Module not found: Can't resolve 'xxx' in 'xxx'Typescript 导入我自己编写的 d.ts 文件的 React js 组件并发生 Module not found: Can't resolve 'xxx' in 'xxx'
【发布时间】:2018-04-05 23:34:24
【问题描述】:

我添加了一个组件js的d.ts文件如下:

/// <reference types="react" />

declare namespace __ReactAvatarEditor {

interface croppingRect {
    x: number,
    y: number,
    width: number,
    height: number
}

interface point {
    x: number,
    y: number
}

export interface AvatarEditorProps {
    scale?: number,
    rotate?: number,
    image?: any,
    border?: number | [number],
    borderRadius?: number,
    width?: number,
    height?: number,
    position?: point,
    color?: number[],
    style?: object,
    crossOrigin?: string,
    onDropFile?: any,
    onLoadFailure?: any,
    onLoadSuccess?: any,
    onImageReady?: any,
    onImageChange?: any,
    onMouseUp?: any,
    onMouseMove?: any,
    onPositionChange?: any,
    disableDrop?: boolean
}
export class AvatarEditor extends React.Component<AvatarEditorProps> {
}
}

declare module "react-avatar-editor"{
    export import AvatarEditorProps = __ReactAvatarEditor.AvatarEditorProps;
    export import AvatarEditor = __ReactAvatarEditor.AvatarEditor;
}
declare module "react-avatar-editor/AvatarEditor"{
    export import AvatarEditor = __ReactAvatarEditor.AvatarEditor;
    export default AvatarEditor;
}

并通过以下方式使用它:

import AvatarEditor from 'react-avatar-editor/AvatarEditor';

源代码构造器:

enter image description here

但是当我运行该项目时,它无法编译并显示错误: ./src/components/DealPic.tsx 未找到模块:无法解析“C:\Users\xiaomin\Desktop\React\inter-provincial-portrait\src\components”中的“react-avatar-editor/AvatarEditor”

Typescript v2.5.3 React v16

【问题讨论】:

  • 在命名空间外部声明该类并导出该类后,它起作用了。但仍有一些错误,我需要调试它

标签: javascript reactjs typescript import declaration


【解决方案1】:

我遇到了同样的问题。要修复它,请以这种格式使用 npm 这个包安装

npm install --save react-avatar-editor @types/react-avatar-editor

【讨论】:

    猜你喜欢
    • 2019-03-11
    • 1970-01-01
    • 1970-01-01
    • 2019-01-14
    • 2019-03-11
    • 2019-05-28
    • 1970-01-01
    • 2014-05-13
    • 2020-04-07
    相关资源
    最近更新 更多