Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2: Why isn't my component's console.log not logging anything

This is my component's code, non of the versions do anything. I just get blank console in browser.

export class AssetsComponent {
    s = 'Hello2';
    constructor() {
        this.s = 'ds';
        console.log(this.s); <--- nothing
        console.log('test'); <--- nothing
        console.log(s); <--- breaks the compiler
    }
}
like image 368
yodalr Avatar asked Aug 14 '17 17:08

yodalr


People also ask

What does console log () do?

The console. log() is a function in JavaScript which is used to print any kind of variables defined before in it or to just print any message that needs to be displayed to the user.

Does console log work in Angular?

log Function in Angular. console. log is a JavaScript function that logs messages to the developer's console or the browser console to the web page. This method helps Angular developers by informing them about the errors, warnings, and what is happening during the execution of their code.


2 Answers

If anyone else encounters this problem here's what my error was: in chrome developer tools, under the console there is a setting to hide all output. For whatever reason, it was turned on. I set it back to "Default" and it works now.enter image description here

like image 92
yodalr Avatar answered Nov 03 '22 00:11

yodalr


It's possible the component isn't being loaded. You didn't include your code showing the whole component file or your app.module file where it should be included. It's also possible it isn't even compiling, because you are trying to access a variable that doesn't exist:

console.log(s); <--- breaks the compiler

There is no variable 's' in the constructor you can access. It needs to be this.s or you need to define a variable s in within the constructor function:

let s = 'something';
like image 30
Notmfb Avatar answered Nov 03 '22 01:11

Notmfb