在angularjs 2中获取一个错误

问题描述:

我想在angularjs2中使用reddit应用程序。在angularjs 2中获取一个错误

得到一个错误:d:/角2/SimpleReddit/src目录/应用/组件/书签交易/ reddit.component.ts(9,13):找不到名称 '文章'

enter image description here

这是我的文件夹结构

enter image description here

Reddit.component.ts

enter image description here

任何一个可以帮助我在此?

首先,你必须输入你想使用

import { Articles } from '../articles'; // Make sure the path is according to what you have in the project structure 

根据您的项目结构,看起来您没有名为Articles的课程。 一条建议,命名类而不是Articles时,将其命名为Article。最好使用单数形式。因此,9号线将看起来像:

articles: Article[]; 

所以,你可能需要做的是你叫article.tsexport class Article { // your properties }Article目录下创建一个新的文件,并将其包含在上面的例子。

+0

我应该提供完整的路径像导入{文章}从'./app/component/Article/article.component'; – JMR

+0

这是正确的吗? – JMR

+0

'从'../ Article/article.component'中导入{Articles};'如果您在Reddit.component.ts'文件中使用它,就应该足够了 – moonflare

您还没有将类文章导入到组件中,没有它,typescript编译器不能为此变量输入类型。

添加import语句导入类:

import { Articles } from './articles'; 

调整路径文章相应文件

+0

我应该从'./app/component/Article/article.component'给出像导入{Articles}这样的完整路径; – JMR

+1

路径应该相对于您当前的文件位置。因此,如果该类位于文章组件文件中,则在文章文件夹中路径将变为:'../Article/article.component' – hagner

+0

如果您想了解有关此主题的更多信息,请参阅打字稿中模块的基本信息。该文件是相当不错的:https://www.typescriptlang.org/docs/handbook/modules.html – glenn