AOT构建错误:装饰器中不支持函数调用

时间:2019-03-12 16:06:37

标签: javascript angular angular-aot

我遇到了一个问题,但我不知道该如何解决,我已经阅读了很多有关该错误的文章,但是我仍然很困惑。我使用nebular/auth程序包创建了我的,而神经包程序完美运行!

我正在使用angular创建一个库。该库用于ionic 3项目。当我想使用ionic-app-scripts 3.2.3构建ionic项目时,出现了一个错误:

“打字稿错误             装饰器不支持'AppModule'函数调用的模板编译过程中的错误,但'RestClient'不支持             被称为。 “

这里的代码:

应用模块

/**
 * @license
 * Copyright Akveo. All Rights Reserved.
 * Licensed under the MIT License. See License.txt in the project root for license information.
 */
import { APP_BASE_HREF } from '@angular/common';
import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { NgModule } from '@angular/core';
import { HttpClientModule, HTTP_INTERCEPTORS, HttpRequest } from '@angular/common/http';
import { CoreModule } from './@core/core.module';

import { environment } from '../environments/environment';
import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';
import { ThemeModule } from './@theme/theme.module';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { NgxAuthModule } from './auth/auth.module';
import { NbOAuth2AuthStrategy , NbOAuth2GrantType, NbOAuth2ClientAuthMethod, NbAuthModule, NbAuthOAuth2JWTToken, NbAuthJWTInterceptor, NB_AUTH_TOKEN_INTERCEPTOR_FILTER } from '@nebular/auth';
import { AuthGuard } from './auth/auth-guard.service';

import { ApolloModule } from 'apollo-angular';
import { HttpLinkModule } from 'apollo-angular-link-http';

//import { WebClientModule, RestClient, GraphQLClient } from './web-client';
import { WebClientModule, RestClient, GraphQLClient } from 'service-consumer';


@NgModule({
  declarations: [
    AppComponent,
  ],
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    HttpClientModule,
    AppRoutingModule,

    NgbModule.forRoot(),
    ThemeModule.forRoot(),
    CoreModule.forRoot(),

    ApolloModule,
    HttpLinkModule,

    NgxAuthModule,
    NbAuthModule.forRoot({
      strategies: [
        NbOAuth2AuthStrategy.setup({ //// IT WORKS !
          name: '*****',
          baseEndpoint: '******',
          clientId: '******',
          clientSecret: '******',
          clientAuthMethod: NbOAuth2ClientAuthMethod.REQUEST_BODY,
          token: {
            endpoint: '/internal/jwt/oauth2/token',
            grantType: NbOAuth2GrantType.PASSWORD,
            class: NbAuthOAuth2JWTToken,
          },
          refresh: {
            endpoint: '/internal/jwt/oauth2/refresh',
            grantType: NbOAuth2GrantType.REFRESH_TOKEN,

          },
        })
      ],
      forms: {},
    }),
    WebClientModule.forRoot({
      clients: [
        RestClient.setup({ //// IT DOES NOT WORK ! WHY ?
          name: 'admin',
          endpoint: environment.BASE_URI + environment.ENDPOINT_CONFIGS.ADMIN,
        }),
        GraphQLClient.setup({
          name: 'graphql',
          endpoint: environment.BASE_URI + environment.ENDPOINT_CONFIGS.GRAPHQL,
        })
      ]
    })
  ],
  bootstrap: [AppComponent],
  providers: [
    { provide: APP_BASE_HREF, useValue: '/' },
    { provide: HTTP_INTERCEPTORS, useClass: NbAuthJWTInterceptor, multi: true},
    {  provide: NB_AUTH_TOKEN_INTERCEPTOR_FILTER, useValue: function (req: HttpRequest<any>) {
        if (req.url === '/api/auth/refresh-token') { // url to define to refresh token
          return true;
        }
        return false;
      },
    },
    //{ provide: HTTP_INTERCEPTORS, useClass: JwtInterceptor, multi: true },
    AuthGuard,
  ],
})
export class AppModule {
}

网络客户端/网络客户端.module

import { Injector, ModuleWithProviders, NgModule, TestabilityRegistry } from '@angular/core';
import { CommonModule } from '@angular/common';

import {
    Client,
    ClientOptions,
    RestClient,
    GraphQLClient
} from './clients';

import {
    CLIENT_SERVICE_OPTIONS,
    CLIENTS_SERVICE,
    ClientServiceOptions,
    ClientClass
} from './web-client-options';

import { ClientService } from './services/client.service';
import { HttpClient, HttpClientModule, HttpHeaders } from '@angular/common/http';

import { Apollo, ApolloModule } from 'apollo-angular';
import { HttpLink, HttpLinkModule } from 'apollo-angular-link-http';
import { InMemoryCache } from 'apollo-cache-inmemory';


/**
 * Rest client factory function
 *
 * @param options rest client options
 * @param injector angular injector service
 */
export function webClientFactory(clientServiceOptions: ClientServiceOptions, injector: Injector): Client[] {
    const clients = [];

    clientServiceOptions.clients
        .forEach(([clientClass, clientOptions]: [ClientClass, ClientOptions]) => {
            if(clientClass == RestClient) {
                const client: Client = new clientClass(injector.get(HttpClient));
                client.setOptions(clientOptions);
                clients.push(client);
            }

            if(clientClass == GraphQLClient) {
                const client: Client = new clientClass(injector.get(Apollo), injector.get(HttpLink));
                client.setOptions(clientOptions);
                clients.push(client);
            }

            // https://github.com/akveo/nebular/issues/682
            // const client: Client = injector.get(clientClass);
            // client.setOptions(clientOptions);
            // clients.push(client);
        });

    return clients;
}

@NgModule({
    imports: [
      CommonModule
    ],
    declarations: [
    ],
    exports: [
    ],
  })
export class WebClientModule {
    static forRoot(clientServiceOptions?: ClientServiceOptions): ModuleWithProviders {
        return <ModuleWithProviders> {
            ngModule: WebClientModule,
            providers: [
               { provide: CLIENT_SERVICE_OPTIONS, useValue: clientServiceOptions },
               { provide: CLIENTS_SERVICE, useFactory: webClientFactory, deps:[CLIENT_SERVICE_OPTIONS, Injector] },
               ClientService,
               RestClient
            ],
        };
    }
}

web-client / rest / rest-client.ts

import { Observable } from 'rxjs';
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';

import { Client } from '../client';
import { ClientClass } from '../../web-client-options';
import { HTTP_VERB } from '../client-request';
import { RestClientRequest } from './rest-client-request';
import { RestClientOptions } from './rest-client-options';


@Injectable()
export class RestClient extends Client {

  // the setup function called in app.module 
    static setup(options: RestClientOptions): [ClientClass, RestClientOptions] {
        return [RestClient, options];
    }

    constructor(private httpClient: HttpClient) {
        super();
    }

    /**
     * Create a Rest Client Request
     * 
     * @return RestClientRequest
     */
    createRequest(httpVerb: HTTP_VERB, path?: string, queryParams: object = {}, content: object = {}): RestClientRequest {
        const request = new RestClientRequest(this.getUriAction(path), queryParams, content);
        request.setHttpVerb(httpVerb);
        return request;
    }

    /**
     * Execute the given request
     * 
     * @param request 
     */
    doRequest(request?: RestClientRequest) : Observable<Object>
    {
        switch(request.getHttpVerb()) {
            case HTTP_VERB.GET: {
                return this.get(request);
            }
            case HTTP_VERB.POST: {
                return this.post(request);
            }
            case HTTP_VERB.PUT: {
                return this.put(request);
            }
            case HTTP_VERB.PATCH: {
                return this.patch(request);
            }
            case HTTP_VERB.DELETE: {
                return this.delete(request);
            }
            default: {
                throw new TypeError(`Invalid HTTP_VERB ${request.getHttpVerb()} given`);
            }
        }
    }

    /**
     * GET http request
     *
     * @param request
     */
    private get(request: RestClientRequest): Observable<Object> {
        return this.httpClient.get(request.getUri() + request.getQueryParams());
    }

    /**
     * POST http request
     *
     * @param path
     * @param body
     */
    private post(request: RestClientRequest): Observable<Object> {
        throw new TypeError(`Not implemented yet`);
    }

    /**
     * PUT http request
     *
     * @param request
     */
    private put(request: RestClientRequest): Observable<Object> {
        throw new TypeError(`Not implemented yet`);
    }

    /**
     * PATCH http request
     *
     * @param request
     */
    private patch(request: RestClientRequest): Observable<Object> {
        throw new TypeError(`Not implemented yet`);
    }

    /**
     * DELETE http request
     *
     * @param request
     */
    private delete(request: RestClientRequest): Observable<boolean> {
        throw new TypeError(`Not implemented yet`);
    }

}

我刚刚提供了有用的代码,希望对我有帮助吗?非常感谢你!

0 个答案:

没有答案