2016-10-14 3 views
3

角度1.5のコンポーネントでtypescriptを使用しています。そして、私はrequire経由でテンプレートを送るカスタムデザインのデコレータを持っています。私はtslintの警告を得る以外は動作します。 (私はこのルールをオフにしたくない)テンプレートを必要とせずにタイプスクリプトにインポート

必要()形式のインポートはここ

を禁止されている私のコンポーネントは

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(); 
    } 
} 

であるここに私のデコレータは

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 })); 
    }; 
}; 
です

テンプレートの代わりにtemplateUrlを渡そうとしましたが、ファイルの実行時エラーが見つかりません。

私は変数としてテンプレートをインポートしようとしましたが、コンパイル時にエラーが発生し、サポートされていないようです。

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

私はwebpackを使用しています。したがって、ルートから絶対パスを使用することはできません。

requireスクリプトを使用せずにtypescriptでテンプレートをインポートする方法はありますか?

+0

私はWebPACKの構築、したがって何のテンプレートファイルやディレクトリ構造がありません使用していますテンプレート –

答えて

1

templateUrlエラーの場合は、参照とディレクトリ構造を確認してください。

あるいは、テンプレート内のHTMLデータを挿入します -

@Component(app, { 
selector: 'mainComponent', 
controllerAs: 'ctrl', 
styleUrls: '', 
template: `<div>//content 
      </div>` 
}) 
+0

をインポート中にエラーを表示することができます。 – Jhankar

+0

webpack build.jsと同じディレクトリにテンプレートを置いてください。おそらくtemplateUrlはbuild.jsのディレクトリの起源からファイルを参照します –

+0

これは動作します。しかし、私はすべてのテンプレート(ディレクトリ内の各コンポーネント用)を配置する必要があります。私の懸念は、ビルドツールとしてwebpackを使用しているときにはこれがベストプラクティスではないかもしれないということです – Jhankar

関連する問題