2017-01-20 34 views
1

我們的Web應用程序(TypeScript 2.1.4)以amd模塊爲目標,並使用///<amd-dependency />將模板加載到requirejs。 今天,我們使用的是帶有成功以下語法的HTML字符串加載到TPL變量:amd-dependency已棄用。如何使用導入「模塊」

/// <amd-dependency path="text!tpl/components/header/header.view.html" name="tpl"/> 
declare var tpl: string; 

此後this directive is deprecated

/// < AMD-依賴性/>

此指令已被棄用。使用導入「moduleName」;代替 報表。

要更換,我們在local.d.ts文件中使用通配符模塊三斜槓指令:

declare module "text!*" { 
    var _: string; 
    export default _; 
} 

,取而代之的指令:

import tpl from "text!tpl/components/header/header.view.html"; 

與編譯tsc很好,但模板的加載失敗。 看起來的JS,編譯生產:

define([text!tpl/components/header/header.view.html, ...],function(header_view_html_1, ...) method 如預期,但隨後使用header_view_html_1。 默認在模塊中。

調試js文件時,模板位於header_view_html_1變量中,而不在header_view_html_1.**default**屬性中,該屬性未定義。

爲什麼typescript產生這個默認屬性?如何避免它,並保持header_view_html_1?

編輯:我跟着導向管從typescriptlang模塊Wildcard module declarations

declare module "json!*" { 
    const value: any; 
    export default value; 
} 
import data from "json!http://example.com/data.json"; 

回答

0

您可能需要調整您的import語句...

這一切都導入到從模塊的別名。

import * as yourAlias from "..." 

這導入特定的部分,並使它們直接可用。

import {thing, anotherThing} from "..." 
+1

我已經嘗試過'進口*爲tpl from「text!tpl/components/header/header.view.html」;'但是它會拋出一個編譯錯誤:錯誤TS2345:類型'typeof「文本的參數!*」'不能分配給'string'類型的參數 – NicoD

0

因爲我們遷移構建系統webpack2,我最終使用html-loader

module: { 
    rules: [ 
     { 
     test: /\.html$/, 
     use: "html-loader" 
     } 

,並加載模板:

let tpl = require("./header.view.html"); 
相關問題