10

我正在使用 TypeScript、Backbone 和 Mustache 编写一个 Web 应用程序。我想使用 Requirejs 进行依赖加载。

我还在使用用于 TypeScript 的 Web Essentials Visual Studio 插件,并打开了 AMD 编译选项。对于那些不熟悉这一点的人,如果您导入外部模块,它会将您的类型脚本文件包装在 AMD 模块中。例如:

在类型脚本中,我在类型定义文件中导入以下模块。

export import Backbone = module("Backbone"); 
import mainTemplate = module("MainTemplate");

输出类似于:

define(["require", "exports", "Backbone", "MainTemplate"], function(require, exports, __Backbone__, __mainTemplate__) {
//...code goes here ...
});

对于模板,我在类型定义文件中声明了以下内容:

declare module "MainTemplate" { }

为了支持 requirejs 插件,您需要将您的模块声明为:

declare module "text!MainTemplate.html" { }

我想保持模块名称没有插件和文件扩展名。这将使我在未来有一些灵活性。

我在require中有以下映射。

require.config({
    map: {
        "MyModule": {
            "MainTemplate": "text!MainTemplate.html"
        }
    }
}

这成功地调用了文本插件,但是插件加载了错误的 url。筛选文本插件的源代码,我发现下面的代码是罪魁祸首。

load: function (name, req, onLoad, config) {
  ...
  url = req.toUrl(nonStripName),
  //returns "scripts/**text!**MainTemplate.html**.html**"
  ...
}

如果我将模块命名为“MainTemplate.html”,它可以正常工作,但我想将扩展名保留在模块名称之外。

我用一个简单的正则表达式替换修改了文本插件,以去除插件引用和重复的扩展名。

有没有更好的方法来处理这个?

4

5 回答 5

11

遇到类似的问题。终于解决了。请参阅TypeScript:编译删除未引用的导入

/// <amd-dependency path="text!templates/application.htm" />

var applicationTemplate = require('text!templates/application.htm');
于 2013-03-28T21:06:15.317 回答
9

对于 Typescript 1.0,这对我有用。首先,我创建了一个 .d.ts 文件,其中存储了每个文本模板的所有模块声明。

//workaround for typescript's lack of support for requirejs text template notation
//remember that a reference to the import variable has to be found in the class, otherwise typescript ignores the import
declare module "text!views/details/details.html" {
    var text: string;
    export = text;
} 
declare module "text!views/layout/layout.html" {
    var text: string;
    export = text;
} 
declare module "text!views/home/home.html" {
    var text: string;
    export = text;
} 

然后引用文本模板,我将这些行添加到类/模块的顶部。

/// <reference path="../texttemplate.d.ts"/>
import detailsTemplate = require('text!views/details/details.html');

The reference line is not actually needed, since the .d.ts file is picked up globally. But I added it as a reminder of the workaround. It also makes it easy to ctrl+click to go the d.ts. file.

于 2014-04-29T09:46:54.107 回答
5

There is a slightly nicer way to do this (I'm using typescript 2.0) Referenced here: https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html

This code expects that your requirejs configuration and plugins are set up correctly:

/// <amd-dependency path="text!./about.html" name="template"/>
declare let template: string;

This helped me a lot to migrate lagacy code to typescript.

于 2016-11-23T20:06:14.620 回答
2

从 TypeScript 0.9.0 开始,我认为您需要执行以下操作:

/// <amd-dependency path="text!templates/application.htm" />
declare var require:(moduleId:string) => any;
var applicationTemplate:string = require("text!templates/application.htm");

在http://www.codebelt.com/typescript/typescript-amd-with-requirejs-tutorial/上查看更多信息

于 2014-03-09T21:20:28.587 回答
0

我们在 TypeScript 应用程序中使用 Backbone 和 require.js。
我们不使用

import backbone = module("Backbone")

语法,而是使用

/// <reference path="../../modules/Backbone.d.ts" />

参考,然后是 BootStrapper。
这样,'text!htmlfile.html' 语法与 require.js 完美配合。

我整理了一篇关于将 require.js 与 TypeScript 和 AMD 结合使用的博客:http: //blorkfish.wordpress.com/2012/10/23/typescript-organizing-your-code-with-amd-modules-and-require -js/

于 2012-12-18T01:22:22.757 回答