0

I'm trying to load js file in my not-found component.

I'm reed information from https://www.truecodex.com/course/angular-6/how-to-use-external-js-files-and-javascript-code-in-angular So, create file, added it in angular.json. In example they show how to do it with onClick event. But I need to load this js file when not-found component is load and I don't know how to do it.

import { Component } from '@angular/core';
declare const notFound: any;

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

    notFound(); // can't use directly - Function implementation is missing or not immediately following the declaration.ts(2391)
}

What I need to add in not-found.component.ts and not-found.component.html to load this script when user load not-found page?

1 Answers1

0

you need to implement the OnInit interface like this:

import {Component, OnInit} from '@angular/core';
declare const notFound: any;

@Component({
  selector: 'app-not-found',
  templateUrl: './not-found.component.html',
  styleUrls: ['./not-found.component.css']
})
export class AppComponent implements OnInit{
  title = 'test';
  ngOnInit() {
      notFound();
  } 

}