8

Component is defined like this:

import {Component} from '@angular/core';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.less']
})
export class AppComponent {

}

I would like to load in some specific components instead of "app.component.html" a file with extention htm "app.component.htm"

@Component({
  selector: 'app-root',
  templateUrl: './app.component.htm',
  styleUrls: ['./app.component.less']
})

from some reason it doesnt work it says:

ERROR in ./app.component.htm 1:0
Module parse failed: Unexpected token (1:0)
You may need an appropriate loader to handle this file type.
<p>
  app component works!
</p>
「wdm」: Failed to compile. 

Please help me find a way to load an htm file? I am aware that there is a way to bootstrap with Webpack, I would like to keep the current build of ng-serve\cli angular.json!

Thank you!

Mike
  • 741
  • 13
  • 40
  • please provide us with the full ts file – AliAb Dec 26 '19 at 10:36
  • becuause according to angular there is no file like htm so not support it only work if you are using angularjs – harkesh kumar Dec 26 '19 at 10:41
  • What forces you to use .htm rather than .html files ? – ethanfar Dec 29 '19 at 06:56
  • There is a process to use a [custom builder](https://github.com/just-jeb/angular-builders/tree/7.x.x/packages/dev-server#builders) to generate a custom webpack configuration that is merged with the existing configuration, allowing you to specify custom loaders for non-standardized file types. I've never actually done it myself, however, so I don't know the exact syntax for what you are needing.... – Claies Dec 29 '19 at 07:05
  • also bear in mind, this appears to be changing in 8.x so it might be a dead end as well.... – Claies Dec 29 '19 at 07:07
  • what is your angular and angular cli version? I try in angular 8 and it is working! @Mike – MHS Dec 29 '19 at 14:39
  • please paste your `webpack.config.js` file here, This can be helpful @Mike – MHS Dec 29 '19 at 14:44
  • @MHS are you sure? on angular8 i can templateUrl htm files? – Mike Dec 29 '19 at 16:48
  • @Mike yes im sure. i test it! – MHS Dec 30 '19 at 05:55
  • @MHS Post it as an answer i will accept it and bounty you. – Mike Dec 30 '19 at 08:51

2 Answers2

2

It may be hard to modify how angular load its template files but you can use @angular-builders/custom-webpack combined with raw-loader import your htm file in component.ts and instead using templateUrl in your component config use template and set it with imported value. The solution is almost described in this SO question with some changes it works for you too :

  1. npm i -D @angular-builders/custom-webpack raw-loader install required packages

  2. Configure angular.json like below :

"build": {
          "builder": "@angular-builders/custom-webpack:browser", // change builder
            "options": {
                "customWebpackConfig": { // add custom config
                     "path": "./extra-webpack.config.js"
                  }, // keep the rest same
  1. Add extra-webpack.config.js file into same directory with angular.json with contents like below :
module.exports = {
  module: {
    rules: [
      {
        test: /\.htm$/, // this just tells use raw-loader to load *.htm files
        use: 'raw-loader'
      }
    ]
  }
};
  1. Add typings.d.ts file into your src folder with content (this will avoid typescript import errors):
declare module '*.htm' {
  const value: string;
  export default value;
}
  1. And in your component import htm file with raw loader
import {Component} from '@angular/core';

import str from 'raw-loader!./app.component.htm';

@Component({
  selector: 'app-root',
  template: str, // notice not url just string
  styleUrls: ['./app.component.css']
})
export class AppComponent {
}

I have managed to run this configuration in my local but can't manage to make it work in stackblitz. Non working Stackblitz example

Community
  • 1
  • 1
Eldar
  • 9,781
  • 2
  • 10
  • 35
2

Upgrade your angular version to angular 8, this is fixed in angular 8.

@Component({
  selector: 'app-root',
  templateUrl: './app.component.htm',
  styleUrls: ['./app.component.less']
})
MHS
  • 881
  • 1
  • 13
  • 30