Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Typescript和AngularJS 1.5:如何处理导出类_Javascript_Angularjs_Typescript - Fatal编程技术网

Javascript Typescript和AngularJS 1.5:如何处理导出类

Javascript Typescript和AngularJS 1.5:如何处理导出类,javascript,angularjs,typescript,Javascript,Angularjs,Typescript,我有这个module.ts文件: import { IHttpService, IPromise } from 'angular'; export class ProductService { static $inject = ["$http"]; constructor(private $http: IHttpService) { } loaded: boolean = false; promise: IPromise<{ data: any }>

我有这个module.ts文件:

import { IHttpService, IPromise } from 'angular';

export class ProductService {
    static $inject = ["$http"];
    constructor(private $http: IHttpService) { }

    loaded: boolean = false;
    promise: IPromise<{ data: any }>;

    getProducts(): IPromise<{ data: any }> {
        if (!this.loaded) {
            this.loaded = true;
            this.promise = this.$http.get('/products.json');
        }
        return this.promise;
    }
}

var module = angular.module("psShopping", ["ngComponentRouter"]);
module.value("$routerRootComponent", "shoppingApp");
module.service('products', ProductService);
有问题的行是:
exports.ProductService=ProductService如果我手动删除此项-应用程序工作正常。但是如果我这样离开,在浏览器控制台中我会得到一个错误“导出未定义”。

对此我们能做些什么?我不能从.ts文件中删除“export”,因为这个类在其他文件中使用(我在那里导入)。我尝试了不同的编译器选项,但它只是给了我不同的错误(如“定义未定义”等),我不知道如何处理这个问题。也许有一种方式tsc不会产生这一行?

每当您在typescript中使用
导入
导出
关键字时,您都会将文件转换为模块

模块设计为与模块系统一起工作,如
commonjs
amd
es6
systemjs

如果您试图在浏览器中加载原始脚本,而不使用模块加载器或绑定器,如systemjs、webpack、browserify、jspm等。。。那么您就不能使用外部模块(因此您不能使用导入/导出)

您仍然可以使用“明确键入”中的键入,但必须通过
/
标记加载它们

如果使用typescript 2.0,则在运行时默认情况下也可以使用并导入angular之类的打字

npm install @types/angular
通常,这些类型将公开全局名称空间声明,您现在可以在整个应用程序中使用这些声明

例如,在您的示例中,angular公开了一个
ng
名称空间,您可以像这样使用它:

  promise: ng.IPromise<{ data: any }>;
请注意,此导入的行为与模块导入不同(它只是别名)


如果您要做的不是一个简单的应用程序,我强烈建议您使用模块加载器(如webpack)为浏览器编译和绑定应用程序。仅使用普通脚本有很多限制。

如何在浏览器中加载模块?您是否正在使用browserify/webpack/jspn等???你需要导出这个类吗?我只是有一个index.html,附带了所有脚本文件。我附加的文件之一就是上面的(module.js)。我在应用程序中只有一个模块(称为psShopping)。在TSConfigyYou中设置
“模块”:“无”
选项,但如果不在浏览器中使用模块加载程序,您将只能完成这一步。您将被限制只导入类型,而不是其他模块。我明白了,是的,现在我得到了“当'--module'为'none'时不能使用导入、导出或模块扩展”。那么我现在如何修改module.ts文件以使其全部工作?或者,我还有什么其他方法可以实现这一点(在浏览器中使用模块加载器?)
  promise: ng.IPromise<{ data: any }>;
import IPromise = ng.IPromise