[英]Angular2/Typescript: access instance variable from the chaining observable function
[英]TypeScript/Angular: Logging instance variable
目前,我正在學習使用TypeScript構建Angular應用程序。 在開發過程中,我創建了一個新對象。 對象的類包含一個變量(設置),我想在joke.module.ts中登錄到控制台。
但是,這不起作用。 我得到的錯誤是:函數實現丟失或沒有緊隨聲明之后。
為什么會這樣,我應該如何記錄呢?
笑話組件
import { Component } from '@angular/core';
@Component({
selector: 'joke',
templateUrl: './joke.component.html'
})
export class JokeComponent {
setup: string;
punchline: string;
constructor() {
this.setup = "What did the cheese say when it looked in the mirror?";
this.punchline = "Halloumi (hello me)"
}
}
笑話。模塊
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { JokeComponent } from './component/joke.component';
@NgModule({
imports: [BrowserModule],
declarations: [JokeComponent],
bootstrap: [JokeComponent]
})
export class JokeModule {
joke = new JokeComponent();
console.log(joke.setup); // error here
}
這是因為您試圖直接在類中而不是在方法中編寫實現:
export class JokeModule {
private joke: JokeComponent;
constructor() {
this.joke = new JokeComponent();
console.log(this.joke.setup); // error here
}
}
另外,我正在為那個私人笑話尋找加分。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.