2017-08-26 44 views
-1

我想在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

任何一個可以幫助我在此?

回答

1

首先,你必須輸入你想使用

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

1

您還沒有將類文章導入到組件中,沒有它,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