将requirejs与Typescript和AMD结合使用的相对路径 [英] Relative paths using requirejs in combination with Typescript and AMD
问题描述
有几个Javascript文件,组织在文件夹Scripts/folder1,Scripts/folder2,...
There are several Javascript files, organized in folders Scripts/folder1, Scripts/folder2, ...
使用requirejs.config.baseUrl将文件夹定义为默认文件夹,例如Scripts/folder1.然后在requirejs.config.paths中,仅使用文件名来寻址某些文件,而使用相对路径来寻址某些文件(例如../folder2/blabla).
With requirejs.config.baseUrl a folder is defined as the default, for example Scripts/folder1. Then in requirejs.config.paths some files are addressed with just the filename, and some are addressed with a relative path (like ../folder2/blabla).
在对Typescipt文件folder2/blabla.ts进行编码时,我们需要folder1中的模块"math".所以我们写
When coding the Typescipt file folder2/blabla.ts we need the module "math" from folder1. So we write
import MOD1 = module("../folder1/math");
关于打字稿,一切都很好.它可以找到模块.但是,对于requirejs来说,这是一个问题.它不知道模块"../folder1/math",只知道"math".
Regarding Typescript, anything is fine with that. It can find the module. However, with requirejs there is a problem. It does not know the module "../folder1/math", it only knows "math".
问题似乎是import语句需要一个文件名,可以从当前目录开始进行处理.但是,这不是requirejs知道的模块ID.
The problem seems to be that the import statement expects a filename, being adressed by starting from the current directory. However, this isn't the module id that requirejs knows about.
在requirejs配置和Typescript中的import语句中的任何地方都使用绝对路径可以解决此问题.
Using absolute paths anywhere, both in the requirejs configuration and the import statement in Typescript, solves the problem.
我做错了吗?还是绝对的路要走? p>
Am I doing this wrong? Or are absolute paths the way to go?
推荐答案
指定baseUrl
等同于Typescript文件的根文件夹:
Specify a baseUrl
to be equivalent to the root folder of your Typescript files:
require.config({
baseUrl: './scripts',
}
)
然后,当您使用从scripts文件夹开始的相对路径时,就可以像通常在typescript中一样进行导入,并且requirejs将使用相同的基本路径.
Then when you use relative paths starting from the scripts folder you can just do import like you normally do in typescript and requirejs will be using the same base path.
更新:此演示文稿应回答您所有的网址/使用来自Typescript问题的js: http ://www.youtube.com/watch?v = 4AGQpv0MKsA ,其代码为: https ://github.com/basarat/typescript-amd/blob/master/README.md
Update: This presentation should should answer all your url / using js from Typescript questions: http://www.youtube.com/watch?v=4AGQpv0MKsA with code : https://github.com/basarat/typescript-amd/blob/master/README.md
这篇关于将requirejs与Typescript和AMD结合使用的相对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!