我使用swagger-codegen和-l typescript-angular
生成 REST 消费者服务库的选项。生成的代码如下所示(DefaultApi.ts
):
namespace API.Client {
'use strict';
export class DefaultApi {
protected basePath = 'http://localhost:7331/v1';
public defaultHeaders : any = {};
static $inject: string[] = ['$http', '$httpParamSerializer', 'basePath'];
constructor(protected $http: ng.IHttpService, protected $httpParamSerializer?: (d: any) => any, basePath?: string) {
if (basePath !== undefined) {
this.basePath = basePath;
}
}
private extendObj<T1,T2>(objA: T1, objB: T2) {
for(let key in objB){
if(objB.hasOwnProperty(key)){
objA[key] = objB[key];
}
}
return <T1&T2>objA;
}
/**
* Delete a person.
* Deletes a specified individual and all of that person's connections.
* @param id The id of the person to delete
*/
public deletePersonById (id: number, extraHttpRequestParams?: any ) : ng.IHttpPromise<{}> {/*...*/}
/* etc... */
}
}
如您所见,有一些具体的类需要使用,但在命名空间内声明,即不能import
。API.Client.DefaultApi
尽管缺少 an ,但我的编辑器(VSCode)在我引用时不会抱怨,import
因为它会将定义作为我认为声明的命名空间的一部分。但是在运行时浏览器抱怨API
没有定义。
我正在使用 webpack 来捆绑我的代码。我在 SO 上看到了其他一些类似于这个的问题,但那里的答案没有运气。
编辑:
根据要求,这是我的 ts 和 webpack 配置文件:
webpack 配置文件:
const webpack = require('webpack');
const conf = require('./gulp.conf');
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const autoprefixer = require('autoprefixer');
module.exports = {
module: {
preLoaders: [
{
test: /\.ts$/,
exclude: /node_modules/,
loader: 'tslint'
}
],
loaders: [
{
test: /.json$/,
loaders: [
'json'
]
},
{
test: /\.(css|less)$/,
loaders: [
'style',
'css',
'less',
'postcss'
]
},
{
test: /\.ts$/,
exclude: /node_modules/,
loaders: [
'ng-annotate',
'ts'
]
},
{
test: /.html$/,
loaders: [
'html'
]
}
]
},
plugins: [
new webpack.optimize.OccurrenceOrderPlugin(),
new webpack.NoErrorsPlugin(),
new HtmlWebpackPlugin({
template: conf.path.src('index.html')
})
],
postcss: () => [autoprefixer],
debug: true,
devtool: 'source-map',
output: {
path: path.join(process.cwd(), conf.paths.tmp),
filename: 'index.js'
},
resolve: {
modules: [
path.resolve(__dirname, '../src/app'),
path.resolve(__dirname, '../node_modules')
],
extensions: [
'',
'.webpack.js',
'.web.js',
'.js',
'.ts'
]
},
entry: `./${conf.path.src('index')}`,
ts: {
configFileName: '../tsconfig.json'
},
tslint: {
configuration: require('../tslint.json')
}
};
tsconfig.json:
{
"compilerOptions": {
"baseUrl": "src/app",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": false,
"module": "commonjs"
},
"compileOnSave": false,
"include": [
"src/**/*.ts"
],
"exclude": [
"!typings/**",
"!node_modules/**"
]
}