【问题标题】:Typescript: *.ts is not a module打字稿:*.ts 不是模块
【发布时间】:2016-12-06 09:30:06
【问题描述】:

我刚刚在我的项目中安装了一个自制的 npm 库。

npm 能够生成每个工件,并且我已经发布了它们。所以,现在我可以使用我提供的服务了:UsersApi。

包结构为:

ts
│───configuration.ts
│───index.ts
│───variables.ts
│
└───api
    ───api.ts
    ───UsersApi.ts

index.ts的内容是:

export * from './api/api';
export * from './variables';
export * from './configuration';

api/api.ts的内容是:

export * from './UsersApi';

而api/UserApi.ts的内容是常规的打字稿内容:

/**
 * Living API
 * desc
 *
 * OpenAPI spec version: 1.0.2
 * 
 *
 * NOTE: This class is auto generated by the swagger code generator program.
 * https://github.com/swagger-api/swagger-codegen.git
 * Do not edit the class manually.
 */

import { Inject, Injectable, Optional }                      from '@angular/core';
import { Http, Headers, URLSearchParams }                    from '@angular/http';
import { RequestMethod, RequestOptions, RequestOptionsArgs } from '@angular/http';
import { Response, ResponseContentType }                     from '@angular/http';

import { Observable }                                        from 'rxjs/Observable';
import 'rxjs/add/operator/map';

//import * as models                                           from '../model/models';
import { BASE_PATH }                                         from '../variables';
import { Configuration }                                     from '../configuration';

/* tslint:disable:no-unused-variable member-ordering */


@Injectable()
export class UsersApi {
    protected basePath = 'http://localhost:8082/commty/cmng';
    public defaultHeaders: Headers = new Headers();
    public configuration: Configuration = new Configuration();

    constructor(protected http: Http, @Optional()@Inject(BASE_PATH) basePath: string, @Optional() configuration: Configuration) {
        if (basePath) {
            this.basePath = basePath;
        }
        if (configuration) {
            this.configuration = configuration;
        }
    }

    /**
     * 
     * Extends object by coping non-existing properties.
     * @param objA object to be extended
     * @param objB source object
     */
    private extendObj<T1,T2>(objA: T1, objB: T2) {
        for(let key in objB){
            if(objB.hasOwnProperty(key)){
                (objA as any)[key] = (objB as any)[key];
            }
        }
        return <T1&T2>objA;
    }

    /**
     * Add user
     * 
     * @param user username
     * @param passwd passwd
     */
    public create(user: string, passwd: string, extraHttpRequestParams?: any): Observable<{}> {
        return this.createWithHttpInfo(user, passwd, extraHttpRequestParams)
            .map((response: Response) => {
                if (response.status === 204) {
                    return undefined;
                } else {
                    return response.json();
                }
            });
    }

    /**
     * User exists
     * 
     * @param user username
     */
    public exists(user: string, extraHttpRequestParams?: any): Observable<boolean> {
        return this.existsWithHttpInfo(user, extraHttpRequestParams)
            .map((response: Response) => {
                if (response.status === 204) {
                    return undefined;
                } else {
                    return response.json();
                }
            });
    }


    /**
     * Add user
     * 
     * @param user username
     * @param passwd passwd
     */
    public createWithHttpInfo(user: string, passwd: string, extraHttpRequestParams?: any): Observable<Response> {
        const path = this.basePath + `/users`;

        let queryParameters = new URLSearchParams();
        let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845
        // verify required parameter 'user' is not null or undefined
        if (user === null || user === undefined) {
            throw new Error('Required parameter user was null or undefined when calling create.');
        }
        // verify required parameter 'passwd' is not null or undefined
        if (passwd === null || passwd === undefined) {
            throw new Error('Required parameter passwd was null or undefined when calling create.');
        }


        // to determine the Content-Type header
        let consumes: string[] = [
        ];

        // to determine the Accept header
        let produces: string[] = [
            'application/json'
        ];





        let requestOptions: RequestOptionsArgs = new RequestOptions({
            method: RequestMethod.Put,
            headers: headers,
            search: queryParameters
        });

        // https://github.com/swagger-api/swagger-codegen/issues/4037
        if (extraHttpRequestParams) {
            requestOptions = this.extendObj(requestOptions, extraHttpRequestParams);
        }

        return this.http.request(path, requestOptions);
    }

    /**
     * User exists
     * 
     * @param user username
     */
    public existsWithHttpInfo(user: string, extraHttpRequestParams?: any): Observable<Response> {
        const path = this.basePath + `/users`;

        let queryParameters = new URLSearchParams();
        let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845
        // verify required parameter 'user' is not null or undefined
        if (user === null || user === undefined) {
            throw new Error('Required parameter user was null or undefined when calling exists.');
        }


        // to determine the Content-Type header
        let consumes: string[] = [
        ];

        // to determine the Accept header
        let produces: string[] = [
            'application/json'
        ];





        let requestOptions: RequestOptionsArgs = new RequestOptions({
            method: RequestMethod.Get,
            headers: headers,
            search: queryParameters
        });

        // https://github.com/swagger-api/swagger-codegen/issues/4037
        if (extraHttpRequestParams) {
            requestOptions = this.extendObj(requestOptions, extraHttpRequestParams);
        }

        return this.http.request(path, requestOptions);
    }

}

我收到下一条消息:

file: 'file:///d%3A/projects/living/user-platform/project/node_modules/cest/ts/api/api.ts'
severity: 'Error'
message: 'File 'd:/projects/living/user-platform/project/node_modules/cest/ts/api/UsersApi.ts' is not a module.'
at: '1,15'
source: 'ts'

这是tsconfig的内容:

{
    "compilerOptions": {
        "target": "es5",
        "module": "commonjs",
        "moduleResolution": "node",
        "sourceMap": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "declaration": true,
        "baseUrl": "./ts",
        "outDir": "js"
    },
    "exclude": [
        "node_modules",
        "js"
    ]
}

有什么想法吗?

【问题讨论】:

  • 可能你应该指定默认导出:"export default class UsersApi {" 或者在 tsconfig 文件中指定模块类型。
  • default 代表什么?以前,我从来没有设置过它,它以前工作过。有可能吗?
  • 每个模块都可以选择导出默认导出。默认导出用关键字 default 标记;每个模块只能有一个默认导出。是的,它是可选的。这不应该影响模块重新引用。
  • 您是否在 tsconfig 文件中为“UsersApi.ts”指定了模块类型?
  • 我刚刚编辑了包含tsconfig 内容的帖子。我刚刚意识到js outDir 文件夹没有生成...

标签: typescript


【解决方案1】:

重建可能会解决问题。

npm run build

我在 Angular 项目中遇到了同样的错误,我找到了 this link。我希望它对以后来的人有所帮助:)

^C
ng serve

【讨论】:

    【解决方案2】:

    我有同样的错误。因为我有两个同一个模块的文件,一个是空文件UsersApi.ts,一个是UsersApi.tsx。

    只删除空的

    【讨论】:

      猜你喜欢
      • 2019-08-24
      • 2017-03-11
      • 2021-05-15
      • 2017-12-10
      • 2023-03-29
      • 1970-01-01
      • 2020-12-06
      • 2021-01-07
      • 2013-12-30
      相关资源
      最近更新 更多