类扩展打字稿错误:"TypeError:Object.setPrototypeOf:应为对象或为null,未定义". [英] Class extending typescript error: "TypeError: Object.setPrototypeOf: expected an object or null, got undefined"

查看:187
本文介绍了类扩展打字稿错误:"TypeError:Object.setPrototypeOf:应为对象或为null,未定义".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my ionic3 app, I have the following pieces of code:

@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: expected an object or null, got undefined

When I try running my app, I get the following error: TypeError: Object.setPrototypeOf: expected an object or null, got undefined

我对此进行了搜索,发现这个问题似乎与同一问题有关.如果我没问题,从此问题页面看来,该错误已在2.4.0或2.4.1版本的打字稿中已修复.
我检查了package.json并在"devDependencies"中找到了"typescript": "~2.4.2".
所以我不明白为什么我仍然有这个错误.

I searched about this, and found this question that seem to relate to the same issue. If I understand it right, from this issue page, it seem that the bug has been fixed in 2.4.0 or 2.4.1 release of typescript.
I checked my package.json and in "devDependencies" I've got "typescript": "~2.4.2".
So I don't understand why I still do have this error.

我是否误解了,此错误可能无法在打字稿2.4.2中修复?如果是,是否有任何解决方法可以使我的代码正常工作?
还是该错误可能来自其他地方?

Did I misunderstand and this bug might not be fixed in typescript 2.4.2? If yes, is there any workaround to make my code work?
Or could that error come from somewhere else?

推荐答案

只需更改您的课程安排:

just change the arrangement of your classes :

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();
  }

} 

检查此 stackblitz

更新:

好,在您的 service.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

这篇关于类扩展打字稿错误:"TypeError:Object.setPrototypeOf:应为对象或为null,未定义".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆