我是一個JavaScript程序員試圖寫打字稿在角2,需要一些幫助:Typscript /角2 - 從一個類將數據傳遞到另一個
我有一個組件,它需要從另一個打字稿文件,然後數據使用它,例如(我知道這是很做作,我想了解如何做這樣的事情):
app.component.ts:
import { exampleArray } from './array';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
constructor(){
this.logToConsole(exampleArrary.arrayProperty);
}
logToConsole(anything:number[]) {
console.log(anything);
}
}
array.ts:
export class exampleArray {
arrayProperty = [1, 2, 3];
}
但我收到此錯誤:
Module 'array' has no exported member 'exampleArray'.)
app.component.ts (12,23): Cannot find name 'exampleArrary'.)
這是接近這樣一個問題的正確方法?有什麼我失蹤?我曾嘗試「new'ing類沒有運氣
1.刪除你不是在你的應用程序組件使用進口Array類的任何地方後保留的數據。 2.數組是數組的基本類型。爲你自己的班級使用另一個名字。請不要選擇數字或字符串。 –
@JBNizet我編輯了這個問題來反映這個問題 –
爲了訪問一個對象的屬性,你需要創建一個對象:'const a = new exampleArray();的console.log(a.arrayProperty);'。如果arrayProperty是'static',那麼你的代碼就可以了,即將有一個單獨的exampleArray鏈接到類本身,而不是鏈接到類的每個實例的新數組。 (如果你正確拼寫exampleArray,即不是'exampleArrary')。請注意,按照慣例,類以大寫字母開頭。 –