Z
Z
zlodiak2018-05-05 21:47:59
Angular
zlodiak, 2018-05-05 21:47:59

Why is the constructor being called repeatedly?

There are two components. One is inherited from the other. An example is here .
As you can see, the constructor of each outputs a phrase to the console. Please explain why the constructor of the HelloComponent component works twice? This can be seen from the message in the console
Is this a normal course of events, can and should this be avoided?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Luzanov, 2018-05-05
@zlodiak

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent extends HelloComponent { // Вот тут
  name = 'Angular 6';

  constructor() {
    super(); // Первый console.log('...')
    console.log('constructor app');
  }
}

You have inherited AppComponent from HelloComponent. Called super(). Then we pushed the HelloComponent into the template again:
<hello name="{{ name }}"></hello> // Второй console.log('...')
<p>
  Start editing to see some magic happen :)
</p>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question