Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import template in typescript without require

I am using typescript with angular 1.5 component. And I have a custom designed decorator where I send template via require. It works except I get a tslint warning. (I don't want to turn off this rule)

require() style import is forbidden

Here is my component

import './main.template.html';

const app: ng.IModule = angular.module('app');
@Component(app, {
  selector: 'mainComponent',
  controllerAs: 'ctrl',
  styleUrls: '',
  template: require('./main.template.html')
})
class MainComponent extends BaseComponent {
   constructor() {
     super();
   }
}

Here is my decorator

export const Component = function(module: ng.IModule, options: {
  selector: string,
  controllerAs?: string,
  styleUrls?: string,
  template?: string,
  templateUrl?: string
}) {
return (controller: Function) => {
   module.component(options.selector, angular.extend(options, {  controller: controller }));
  };
};

I have tried to pass templateUrl instead of template and i get run time error of file not found.

I have also tried to import template as variable and it gives compile time error and looks like it's not supported.

import mytemplate: string from './main.template.html';

I am using webpack. Hence, can't use absolute path from the root.

Do you have any idea how to import template in typescript without using require.

like image 585
Jhankar Avatar asked Aug 01 '26 18:08

Jhankar


1 Answers

For templateUrl error , check your reference and directory structure.

Or , insert html data inside template :-

@Component(app, {
selector: 'mainComponent',
controllerAs: 'ctrl',
styleUrls: '',
template: `<div>//content
           </div>`
})
like image 98
Anmol Mittal Avatar answered Aug 03 '26 09:08

Anmol Mittal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!