Using console.log() with Angular

2017-06-28  本文已影响15人  RoyTien

From: https://stackoverflow.com/questions/37869496/console-log-not-working-in-angular2-component-typescript
Author: Daniel Pliscki

It's not working because console.log() it's not in a "executable area" of the class "App".

A class is a structure composed by attributes and methods.

The only way to have your code executed it's to place it inside a method that is going to be executed. For instance: constructor() or ngOnInit()

console.log('It works here')

@Component implements OnInit({..)
export class App {
  s: string = "Hello";
            
  constructor() {
    console.log(this.s)            
  }

  ngOnInit(){
    console.log(123);
  }
}
上一篇下一篇

猜你喜欢

热点阅读