Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 类扩展typescript错误:";TypeError:Object.setPrototypeOf:应为对象或null,未定义";_Angular_Typescript_Ionic2 - Fatal编程技术网

Angular 类扩展typescript错误:";TypeError:Object.setPrototypeOf:应为对象或null,未定义";

Angular 类扩展typescript错误:";TypeError:Object.setPrototypeOf:应为对象或null,未定义";,angular,typescript,ionic2,Angular,Typescript,Ionic2,在我的ionic3应用程序中,我有以下几段代码: @Injectable() // I tried also without this @Injectable and got the same error export class M_someClass { constructor () { } method1 () { console.log("method1 used") } } @Injectable() export class M_otherClass extend

在我的ionic3应用程序中,我有以下几段代码:

@Injectable() // I tried also without this @Injectable and got the same error
export class M_someClass {
  constructor () {

  }

  method1 () { console.log("method1 used") }

}

@Injectable()
export class M_otherClass extends M_someClass {

  callMethod1 () {
    this.method1()
  }

}
当我尝试运行我的应用程序时,出现以下错误:
TypeError:Object.setPrototypeOf:应为对象或null,未定义

我搜索了一下,发现似乎和同一个问题有关。如果我理解正确的话,这个bug似乎已经在typescript的2.4.0或2.4.1版本中修复了。
我检查了我的package.json,在“devdependences”中我得到了
“typescript”:“~2.4.2”

所以我不明白为什么我仍然有这个错误

我是否误解了typescript 2.4.2中可能无法修复此错误?如果是,是否有任何变通方法使我的代码正常工作?

或者该错误可能来自其他地方吗?

只需更改您的课程安排:

import { Component } from '@angular/core';

class M_otherClass  {
  constructor(){

  }

  test(){
    console.log('test')
  }

}


@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent extends M_otherClass  {
  constructor(){
    super();
    this.test();
  }

} 
检查这个

更新

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { AppComponent} from './app.component';
import { HelloComponent } from './hello.component';
import { ChildService, ParentService } from './app.service.ts'

@NgModule({
  imports:      [ BrowserModule, FormsModule ],
  declarations: [ AppComponent, HelloComponent ],
  bootstrap:    [ AppComponent ],
  providers: [ ChildService, ParentService]
})
export class AppModule { }
好的,在您的服务中。ts

import { Injectable } from '@angular/core';

@Injectable()
class ParentService{
  constructor(){}
  get(){console.log('test')}
}

@Injectable()
class ChildService extends ParentService{
  constructor (){
    super();
  }
}

export {
    ChildService,
    ParentService
}
在使用服务的组件中:

import { Component } from '@angular/core';
import { ChildService } from './app.service.ts'


@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent {
  constructor(public childService:ChildService){
    this.childService.get();
  }
}
在您的app.module.ts中:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { AppComponent} from './app.component';
import { HelloComponent } from './hello.component';
import { ChildService, ParentService } from './app.service.ts'

@NgModule({
  imports:      [ BrowserModule, FormsModule ],
  declarations: [ AppComponent, HelloComponent ],
  bootstrap:    [ AppComponent ],
  providers: [ ChildService, ParentService]
})
export class AppModule { }

检查这项工作

我不确定我是否理解你的建议。我正在创建的两个类是提供者,因此,它们需要
@可注入的
装饰器,所以我不太明白使用
@组件
是如何工作的。我已经深入检查了它,我的代码的形状与您的建议完全相同。在stackblitz中尝试,它似乎在那里起作用。所以我试着更新我的ionic以防万一,运行
npm install-g-ionic
,但是我仍然得到同样的错误。我想知道我是否应该在我的应用程序中更新一些东西。@chateau怎么了?你发现问题了吗?我花了一些时间清理我的整个代码,以确保其他一切都是好的。再次检查之后,问题仍然存在。我在我的目录上运行了一个npm安装,但没有对其进行任何更改。我没有太多其他的线索。我正在试图弄清楚我是否必须找到一个解决这个bug的方法,而不是使用类扩展。但是对于代码的质量来说,这真的是一个遗憾。