【发布时间】:2017-03-30 02:30:14
【问题描述】:
我有这个 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);
这会被转译/编译成 module.js:
"use strict";
var ProductService = (function () {
function ProductService($http) {
this.$http = $http;
this.loaded = false;
}
ProductService.prototype.getProducts = function () {
if (!this.loaded) {
this.loaded = true;
this.promise = this.$http.get('/products.json');
}
return this.promise;
};
ProductService.$inject = ["$http"];
return ProductService;
}());
exports.ProductService = ProductService;
var module = angular.module("psShopping", ["ngComponentRouter"]);
module.value("$routerRootComponent", "shoppingApp");
module.service('products', ProductService);
有问题的行是:exports.ProductService = ProductService; 如果我手动删除它 - 应用程序可以完美运行。但是,如果我这样离开,在浏览器控制台中我会收到错误“未定义导出”。
关于这个还能做什么?我不能只从 .ts 文件中删除“导出”,因为此类用于其他文件(我将其导入那里)。我尝试了不同的编译器选项,但它只是给了我不同的错误(比如“未定义定义”等),我不知道如何处理这个问题。也许有办法让 tsc 不生产这条线?
【问题讨论】:
-
你是如何在浏览器中加载模块的?你在使用 browserify/webpack/jspn 等吗???需要导出类吗?
-
我只有一个附有所有脚本文件的 index.html。我附加的文件之一是上面的文件(module.js)。我的应用中只有一个模块(称为 psShopping)。
-
在你的 tsconfig 中设置
"module" :"none"选项 -
您只能在浏览器中不使用模块加载器的情况下获得这么多。您将只能导入类型,而不是其他模块。
-
我明白了,是的,现在我得到“当 '--module' 为 'none' 时,不能使用导入、导出或模块扩充。”那么我现在如何修改 module.ts 文件以使其全部工作?或者我可以通过哪些其他方式来完成这项工作(在浏览器中使用模块加载器?)
标签: javascript angularjs typescript