2016-01-15 28 views
1

我有两个文件comment.tsx和commentlist.tsx进口类+反应过来

comment.tsx看起来是这样的:

/// <reference path="./typings/react/react.d.ts" /> 
/// <reference path="./typings/react/react-dom.d.ts" /> 
/// <reference path="interfaces.d.ts" /> 

import * as React from "react"; 
import * as ReactDOM from "react-dom"; 

namespace W.T.F { 
    export class Comment extends React.Component<ICommentData, {}>{ 
     constructor(props: ICommentData) { 
      super(props); 
     } 

     public render() { 
      return (
       <div className="comment"> 
       <h2 className="commentAuthor">{this.props.Author}</h2> 
       <span>{this.props.Text}</span> 
        </div> 
      ); 
     } 
    } 
} 

和commentlist.tsx

/// <reference path="./typings/react/react.d.ts" /> 
/// <reference path="./typings/react/react-dom.d.ts" /> 
/// <reference path="comment.tsx" /> 
/// <reference path="interfaces.d.ts" /> 

import * as React from "react"; 
import * as ReactDOM from "react-dom"; 
import Comment = W.T.F.Comment; 

namespace W.T.F { 
    export class CommentList extends React.Component<ICommentListData, {}>{ 
     public render() { 
      var commentNodes = this.props.Data.map(function (comment: ICommentData) { 
       return (
        <Comment Author={comment.Author} Text={comment.Text}>{comment.Text}</Comment> 
       ); 
      }); 
      return (
       <div className="commentList"> 
        {commentNodes} 
       </div> 
      ); 
     } 
    }; 
} 

编译这些文件所带来的误差

commentlist.tsx(8,24)TS2305模块“WT .F'没有导出成员'评论'。

我的来源有什么问题?

从其他文件导入其他反应类(包含在名称空间中)的最佳方法是什么?

+1

现在你的两个文件都是“外部模块”。在“命名空间W.T.F”中移动导入语句。您可能需要将其更改为“声明命名空间...”。 – Louy

+1

结帐**陷阱** https://github.com/Microsoft/TypeScript-Handbook/blob/master/pages/Namespaces%20and%20Modules.md。如果你想使命名空间的工作,你必须导出它们afaik。 – FlorianTopf

+0

hm ..我看到我必须删除外部模块中的命名空间,但是.tsx文件作为外部模块处理和.ts文件处理的原因是什么? – Henk

回答

0

如果有任何顶级导入,并且示例中的名称空间成为模块的一部分,TypeScript将该文件视为外部模块。

大多数项目将需要使用外部模块没有命名空间,但使用的命名空间你想要的方式,这样使用旧的参考/进口语法,与命名空间内定义的进口:

/// <reference path="./typings/react/react.d.ts" /> 
namespace W.T.F { 
    import React = __React; 

    export class Comment extends React.Component<ICommentData, {}>{ 
     constructor(props: ICommentData) { 
      super(props); 
     } 

     public render() { 
      return (
       <div className="comment"> 
       <h2 className="commentAuthor">{this.props.Author}</h2> 
       <span>{this.props.Text}</span> 
        </div> 
      ); 
     } 
    } 
} 
+1

谢谢。编辑它以符合您的反馈 –

+0

感谢您的回答。我删除了当前项目中的名称空间。我会在下次尝试您的解决方案来验证它。 – Henk