Include htm template into another one on Angular 6

Viewed 5834

Actually, i have one HTML file which contains all the code. I want to split this one on multiple files and to include them.

How can i do this?

Thanks a lot.

3 Answers

Let's say you have this html:

<div class="componentA">ComponentA</div>
<div class="componentB">ComponentB</div>

and this code is into the `AppComponent. You can split this two div into two component:

ComponentA.ts

@Component({
  selector: 'componentA',
  templateUrl: 'componentA.component.html',
  styleUrls: ['componentA.component.scss'],

)}
export class ComponentA {

}

ComponentA.html

<div class="componentA">ComponentA</div>

ComponentB.ts

@Component({
  selector: 'componentB',
  templateUrl: 'componentB.component.html',
  styleUrls: ['componentB.component.scss'],

)}
export class ComponentB {

}

ComponentB.html

<div class="componentB">ComponentB</div>

then into your AppComponent.html :

<componentA></componentA>
<componentB></componentB>

In addition to answers above, don't forget to include

import { Component } from '@angular/core';

in your both components ts file.

Related