1

In my angular 15 app I have many components that use the same template, by referencing to the same template file in TemplateUrl.

for example:

@Component({
  selector: 'app-my-comp',
  templateUrl:'../../myTemplate.html',// this is a common template,
})
export class MyComponent {
}

For some reason I need to take  the  myTemplate.html of the app, and set it in an external library in my project.

  1. How can I export a HTML template from a library (in a library's public API I can export only ts files)?

  2. How can I use it in the TemplateUrl of MyComponent component?

1 Answer 1

1

maybe I am wrong but I don't think this is possible. Also using the same Template in multiple components is something to use with caution.

Maybe you should explain your use-case and then we can look for different solutions/architectures.

For example if you need the same template but different functionality, you should move the UI in a so-called "dumb component" and re-use it in multiple smart components.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your reply. Most of the pages in our site use the same template, with a different data. So their template can be a common template, and each of them extends a common base component.ts, that makes the common busyness. Each page sets its data by overriding the base component fields, and can add its own. You are correct that we can move it to a dumb component and each page will have an instance of this dumb in its html, but isn't it unnecessary if most of the html of the pages have only one element, the dumbComponent ? Thanks in advance!
I would say that if you have many component that extend a base component, or if you have many component that contains only one dumb component, in both cases, the complexity, size and performance would be about the same, but the second option is more clean & maintainable in my opinion. You don't need a HTML pages since you can declare the template directly in your .ts file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.