48

I am relatively new to both Angular2 and typescript. Since typescript is a superset of javascript, I'd expect functions like console.log to work. console.log works perfectly in .ts files when outside a component class but does not work as I'd expect from inside the component class.

// main.ts

import { Component } from '@angular/core';
console.log("Hello1"); //1. This works perfectly

@Component({..)
export class App {
 s: string = "Hello2";
 // console.log(s); //2. This gives compilation error (when uncommented)
 // Error: Function implementation is missing or not immediately following the declaration.
}

Is there anything that I am missing?

3
  • 1
    what did you put in the @Component? Can you try and put a constructor in the class like this: constructor() { console.log('test')} Commented Jun 16, 2016 at 21:19
  • 1
    i think it's not working because console.log is not wrapped in a function. can you show the JS compiled form ? Commented Jun 16, 2016 at 21:21
  • @L.querter: console.log is working when it's used inside the constructor but doesn't work when used outside the constructor even if constructor is present. Maybe wrapping it in a function is necessary. I wasn't aware of this. Commented Jun 16, 2016 at 21:26

2 Answers 2

83

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 is to place it inside a method that is going to be executed. For instance: constructor()

console.log('It works here')

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

Think of class like a plain javascript object.

Would it make sense to expect this to work?

class:  {
  s: string,
  console.log(s)
 }

If you still unsure, try the typescript playground where you can see your typescript code generated into plain javascript.

https://www.typescriptlang.org/play/index.html

Sign up to request clarification or add additional context in comments.

5 Comments

A variable initialization is also an expression and I am confused why that works outside the constructor's scope and function calls do not.
Try that typescript playgroung I mentioned above, you'll see that every variable definition is transpilled to a javascript function body.
something so obvious and I was starting to go crazy trying to solve this - thanks
In the javascript example you gave, it still makes no sense that s:string, console.log(s) shouldn't return at least some value. It should at least say in the browser s is of type string.
Wow, in an hour of searching, this answer finally made it click in my mind. Thank you! Angular newbie here.
7

The console.log should be wrapped in a function , the "default" function for every class is its constructor so it should be declared there.

import { Component } from '@angular/core';
console.log("Hello1");

 @Component({
  selector: 'hello-console',
})
    export class App {
     s: string = "Hello2";
    constructor(){
     console.log(s); 
    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.