[英]TypeScript with require.js and jquery does not work on production server (require.js Script Error)
我最近開始研究TypeScript。 我正在使用require.js加載模塊。 目前,我僅加載jquery,但是顯然它將得到擴展。
我的項目設置如下:
app
> classes
> Greeter.ts
> AppConfig.ts
> AppMain.ts
lib
> jquery-1.7.2.js
> require.js
modules
> jquery.d.ts
> require.d.ts
app.ts
default.htm
AppConfig.ts
/// <reference path="../modules/require.d.ts" />
/// <reference path="AppMain.ts" />
require.config({
baseUrl: '../',
paths: {
'jquery': 'lib/jquery-1.7.2'
},
shim: {
jquery: {
exports: '$'
}
}
});
require(['jquery','app/AppMain'],
($, main) => {
// code from window.onload
var appMain = new main.AppMain();
appMain.run();
});
AppMain.ts
import gt = module("app/classes/Greeter");
export class AppMain {
public run() {
var el = document.getElementById('CONTENT_PH');
var greeter = new gt.Greeter(el);
greeter.start();
}
}
迎賓員
export class Greeter {
element: HTMLElement;
span: HTMLElement;
timerToken: number;
constructor (element: HTMLElement) {
this.element = element;
this.element.innerText += "The time is: ";
this.span = document.createElement('span');
this.element.appendChild(this.span);
this.span.innerText = new Date().toUTCString();
}
start() {
this.timerToken = setInterval(() => this.span.innerText = new Date().toUTCString(), 500);
}
stop() {
clearTimeout(this.timerToken);
}
}
.csproj
<PropertyGroup Condition="'$(Configuration)' == 'Debug'">
<TypeScriptSourceMap> --module AMD</TypeScriptSourceMap>
</PropertyGroup>
<Target Name="BeforeBuild">
<Message Text="Compiling TypeScript files" />
<Message Text="Executing tsc$(TypeScriptSourceMap) @(TypeScriptCompile ->'"%(fullpath)"', ' ')" />
<Exec Command="tsc$(TypeScriptSourceMap) @(TypeScriptCompile ->'"%(fullpath)"', ' ')" />
</Target>
我有一些在本地計算機上工作的示例,但是當我嘗試在實時服務器上運行它時,require.js提供了以下腳本錯誤。
錯誤:腳本錯誤http://requirejs.org/docs/errors.html#scripterror [中斷此錯誤]
var e = new Error(msg +'\\ nhttp://requirejs.org/docs/errors.html#'+ id); require.js(第194行)
我不知道為什么這可以在我的開發機器上工作,而不是在實時服務器上工作。
在chrome和Internet Explorer中本地運行此示例有效,但在Firefox中則無效。 但這可能是另一個問題。
如果您需要其他詳細信息,請告訴我。
部署到服務器時,是否以發布模式編譯網站?
在這種情況下,您需要在項目文件中添加發行版本:
<PropertyGroup Condition="'$(Configuration)' == 'Release'">
<TypeScriptSourceMap> --module AMD</TypeScriptSourceMap>
</PropertyGroup>
<Target Name="BeforeBuild">
<Message Text="Compiling TypeScript files" />
<Message Text="Executing tsc$(TypeScriptSourceMap) @(TypeScriptCompile ->'"%(fullpath)"', ' ')" />
<Exec Command="tsc$(TypeScriptSourceMap) @(TypeScriptCompile ->'"%(fullpath)"', ' ')" />
</Target>
在TypeScript 0.8.2+中,格式為:
<PropertyGroup Condition="'$(Configuration)' == 'Debug'">
<TypeScriptTarget>ES5</TypeScriptTarget>
<TypeScriptIncludeComments>true</TypeScriptIncludeComments>
<TypeScriptSourceMap>true</TypeScriptSourceMap>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)' == 'Release'">
<TypeScriptTarget>ES5</TypeScriptTarget>
<TypeScriptIncludeComments>false</TypeScriptIncludeComments>
<TypeScriptSourceMap>false</TypeScriptSourceMap>
</PropertyGroup>
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\TypeScript\Microsoft.TypeScript.targets" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.