Reputation: 21
If I create a library with the following configuration, install it on the application side, and inherit and use it,
If ng build --prod=true
, ngOnInit
will not be called.
If ng build
, ngOnInit
is called.
Angular
: 6.1.1Angular CLI
: 6.2.9It works if you quit the library and put the library source into the application. However, I want to make it a library because I want to use it in common.
export abstract class LibrarySampleBase implements OnInit {
public ngOnInit() {
this.InitPage();
}
}
export abstract class LibrarySample extends LibrarySampleBase {
// no exist ngOnInit()
}
@Component({
selector: 'app-sample',
templateUrl: './sample.component.html',
styleUrls: ['./sample.component.css']
})
export class Sample extends LibrarySample {
InitPage() {
// processing
}
}
How can I get ngOnInit
to be called even when ng build --prod=true
?
Also, please let me know if you have any bug information.
Upvotes: 1
Views: 297
Reputation: 3149
This is working for me:
import { OnInit } from '@angular/core';
export abstract class LibrarySampleBase implements OnInit {
public ngOnInit() {
this.initPage();
}
initPage() {
console.log('Hi');
}
}
export abstract class LibrarySample extends LibrarySampleBase {}
import { Component } from '@angular/core';
import { LibrarySample } from './library-sample-base.ts';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent extends LibrarySample {
initPage() {
console.log('Bye!');
}
}
Bye is shown as the log in the console. If I comment the initPage()
in the AppComponent Hi is logged instead. So I think that works as expected.
Angular
: 7.2.12Angular CLI
: 7.3.8I'm using ng serve --prod=true
to try it.
Upvotes: 1