2017-01-20 37 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"); 
相关问题