简体   繁体   English

Typescript AMD外部模块加载,角度未定义

[英]Typescript AMD external module loading, angular is not defined

I know that there are some similar questions on this topic already, but somehow none of them helped. 我知道这个主题已经有一些类似的问题,但是它们都无济于事。 I want to use angular.js in a project which uses requirejs and Typescript. 我想在使用requirejs和Typescript的项目中使用angular.js。

I have a module, where I use angular to create a service module: 我有一个模块,在这里我使用angular来创建服务模块:

/// <reference path="../../../typings/angularjs/angular.d.ts" />
...
var services = angular.module('services', []);
...
export = services;

This code compiles withouth error, but in the created js file, there is angular.js dependency is not there: 这段代码编译没有错误,但是在创建的js文件中,没有angular.js依赖项:

define(["require", "exports"], function(require, exports) {
...
}

And when I run the application, the browser complains: Uncaught ReferenceError: angular is not defined 当我运行该应用程序时,浏览器会抱怨:Uncaught ReferenceError:未定义角度

My guess is, that somehow I should import the angular besides referencing it, but none of the path's I tried works. 我的猜测是,除了引用它之外,我还应该以某种方式导入该角度,但是我尝试过的所有路径均无用。

Here is my require.js configuration, in case it is needed: 这是我的require.js配置,以备不时之需:

require.config({

    paths: {
        angular: '../lib/angular/angular',
        ...
    },
    shim: {
        angular : {exports : 'angular'},
        ...
    }
)}

Can u help me, what is missing here? 你能帮我吗,这里缺少什么? Thanks. 谢谢。

When you use a reference comment: 使用参考注释时:

/// <reference path="../../../typings/angularjs/angular.d.ts" />

You are telling the compiler "I will ensure this dependency is available at runtime". 您告诉编译器“我将确保在运行时可以使用此依赖项”。

If you want the module to be loaded, you need to use an import instead... 如果要加载模块,则需要使用导入...

import angular = require('angular');

The zero-set-up way of doing this is to place the angular.d.ts file next to the actual angular.js file, so the path you specify in the import statement is the same either way - otherwise, use your require config to shimmy the path. 零建立这样做的方式是将angular.d.ts下一个文件到实际angular.js文件,所以你在导入语句中指定的路径是相同的两种方式-否则,使用您需要的配置摆弄路径。

Have you tried adding the following to the top of your file 您是否尝试过将以下内容添加到文件顶部

 /// <amd-dependency path="angular"/>

This should make ts compiler add angular to the define list. 这应该使ts编译器将angular添加到定义列表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM