目前我正在学习使用TypeScript构建Angular应用程序。在开发过程中,我创建了一个新对象。对象的类包含一个变量(setup),我想在joke.module.ts中登录到控制台。
但是,这不起作用。我得到的错误是:在声明之后缺少或不立即执行函数实现。
为什么会这样,我该如何记录?
joke.component.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)"
}
}
joke.module.ts
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
}
答案 0 :(得分:0)
这是因为你试图直接在类中编写实现,而不是在方法中编写:
export class JokeModule {
private joke: JokeComponent;
constructor() {
this.joke = new JokeComponent();
console.log(this.joke.setup); // error here
}
}
另外,我正在为那个私人笑话寻找奖励积分。