Home > Blockchain >  Angular - Member 'console' implicitly has an 'any' type
Angular - Member 'console' implicitly has an 'any' type

Time:11-10

Below is my code in angular. Simply trying to output to log an env var

import { Component } from '@angular/core';
import { environment } from '../environments/environment';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent  {
  title = 'angular';

  

  console.log( environment.API_URL);
}

However, I get the below error:

Error: src/app/app.component.ts:12:3 - error TS7008: Member 'console' implicitly has an 'any' type.

12   console.log( environment.API_URL);
     ~~~~~~~

How do I resolve this? Should not angular know its a object key?

CodePudding user response:

As the comments said, you have to place it in a method an and execute it. Here an example using the Lifecycle and in addition the constructor:

import { Component, Input, OnInit } from '@angular/core';
import { environment } from '../environments/environment';

export class AppComponent implements OnInit  {
  title = 'angular';

  constructor() {
    console.log( environment.API_URL);
  }

  ngOnInit():void {
    console.log( environment.API_URL);
  }
}

CodePudding user response:

you should put the console log inside a function or in a lifecycle hook.

  • Related