AngularJS 2 TypeScript编译错误 - 类型

时间:2016-12-20 01:39:20

标签: angular typescript webpack

我已经检查过我的问题的所有可能解决方案,这些解决方案基本上是指TypeScript编译。每次我启动项目时都会有这些:

Argument of type '{ declarations: (typeof NavbarComponent | typeof FooterComponent | typeof AboutComponent | typeof...' is not assignable to parameter of type 'NgModule'.
  Types of property 'providers' are incompatible.
    Type '(typeof BaseRequestOptions | typeof DataService | typeof UserService | typeof AuthenticationServi...' is not assignable to type 'Provider[]'.
      Type 'typeof BaseRequestOptions | typeof DataService | typeof UserService | typeof AuthenticationServic...' is not assignable to type 'Provider'.
        Type 'typeof DataService' is not assignable to type 'Provider'.
          Type 'typeof DataService' is not assignable to type 'FactoryProvider'.
            Property 'provide' is missing in type 'typeof DataService'.
[default] /SYS/PROJECT/WEB/studio/front-studio/src/app/app.routing.ts:14:7
    Type '({ path: string; component: typeof LoginComponent; } | { path: string; component: typeof HomeComp...' is not assignable to type 'Route[]'.
  Type '{ path: string; component: typeof LoginComponent; } | { path: string; component: typeof HomeCompo...' is not assignable to type 'Route'.
    Type '{ path: string; component: typeof HomeComponent; }' is not assignable to type 'Route'.
      Types of property 'component' are incompatible.
        Type 'typeof HomeComponent' is not assignable to type 'Type<any>'.
          Cannot assign a 'private' constructor type to a 'public' constructor type.
[default] Checking finished with 2 errors

我试图在组件中添加一个Type,但它仍然是相同的。该应用程序运行正常,所以我猜它是一个TypeScript编译问题。

路由器还在Sublime Text中显示错误:

const appRoutes : Routes = [

      {     path: 'login', component: LoginComponent },

      {     path: '', component: HomeComponent },
      {     path: 'about', component: AboutComponent, canActivate: [AuthGuard] },

    // Otherwise redirect to home
    { path: '**', redirectTo: '' }
    //{ path: '**', component: PageNotFoundComponent }
];

export const routedComponents = [HomeComponent, AboutComponent];
export const routing : ModuleWithProviders = RouterModule.forRoot(appRoutes);

这是我的tsconfig,如果有帮助的话:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": [ "es2015", "dom" ],
    "noImplicitAny": true,
    "suppressImplicitAnyIndexErrors": true,
    "typeRoots": [
       "../node_modules/@types"
     ]
  },
  "exclude": [
        "node_modules",
        "typings/main",
        "typings/main.d.ts"
    ]
}

我还能尝试什么?我从各处删除了指令,每个组件都在@ngModule声明中。我花了差不多一个星期,但我仍然没有找到任何解决办法。

@ngModule:

...

import { routing } from './app.routing';
import { AppComponent } from './app.component';
import { Component, Type } from '@angular/core';

@NgModule({

  // Components and directives
  declarations: [
    AppComponent,
    NavbarComponent, HomeComponent, ...
  ],

  // Module dependencies
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    routing,
    SelectModule,

  ],

  schemas: [ CUSTOM_ELEMENTS_SCHEMA ],

  // Services
  providers: [

        DataService,
        ...


        ],

  // Root component
  bootstrap: [AppComponent],

})

export class AppModule {

}

3 个答案:

答案 0 :(得分:9)

我有同样的问题,在我的情况下它发生了,因为我在相应的服务中使用私有构造函数。所以解决方案是使protocol CanPreventPopProtocol { func shouldBePopped() -> Bool } class MyNavigationController: UINavigationController { override func popViewController(animated: Bool) -> UIViewController? { let viewController = self.topViewController if let canPreventPop = viewController as? CanPreventPopProtocol { if !canPreventPop.shouldBePopped() { return nil } } return super.popViewController(animated: animated) } //important to prevent UI thread from freezing // //if popViewController is called by gesture recognizer and prevented by returning nil //UI will freeze after calling super.popViewController //so that, in order to solve the problem we should not return nil from popViewController //we interrupt the call made by gesture recognizer to popViewController through //returning false on gestureRecognizerShouldBegin // //tested on iOS 9.3.2 not others func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool { let viewController = self.topViewController if let canPreventPop = viewController as? CanPreventPopProtocol { if !canPreventPop.shouldBePopped() { return false } } return true } } 中的构造函数非私有,即删除DataService修饰符。

答案 1 :(得分:1)

我对私有构造函数AppComponent有同样的问题。通过使构造函数公开来解决它

答案 2 :(得分:0)

另外,检查您是否在父/基类中添加了'abstract',并且应该将其删除。