0

I have my own NgModule published and installed via npm. Currently the template of the component in the imported module has a inline template. The template got huge (200+ LOC) over time and it would be nice to use the templateUrl instead of inline template in the component.

So instead of this:

template: `<h1>My template</h1>`,

I want to use this:

templateUrl: './my-template.html'

All the solutions explained in the folowing post don't work if the module/component is imported via node_modules. https://blog.thoughtram.io/angular/2016/06/08/component-relative-paths-in-angular-2.html

I know there are some solutions with gulp packages, that resolve the path and put the template as inline string, but is there any other solution to this?

Edit (add more code examples):

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

@Component({
    selector: 'my-component',
    templateUrl: './my-template.html'
})
export class MyComponent {
    constructor() {
    }
}

The template is in the same directory as the ts file. It works if I move the directory from node_modules/my-package/my-component and put it anywhere in src/app/.

8
  • can you post more code? (An example of your Component would really suffice) Commented Feb 1, 2017 at 9:49
  • Component logic can be empty. It is really just the template url in npm components. The Component works if you copy the component from the node_modules folder to your src/app without changing a single line. Commented Feb 3, 2017 at 13:07
  • well not really , if you're to use a relative path the module: module.id has to be set in your @Component decorator, otherwise Angular 2 will be looking for your files at the root level Commented Feb 3, 2017 at 13:42
  • I am using webpack. If i use module: module.id the application won't start with the error Error: moduleId should be a string in MyModule. And yes you are right, Angular is looking at the root level for the files Commented Feb 6, 2017 at 8:46
  • so can you share a short example of your Component? I'ts really hard to come up with any solution if there is no code to see. Commented Feb 6, 2017 at 9:39

0

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.