angular - 类扩展打字稿错误:“TypeError:Object.setPrototypeOf:期望一个对象或null,未定义”
问题描述
在我的 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: expected an object or null, got undefined
我对此进行了搜索,发现这个问题似乎与同一问题有关。如果我理解正确,从这个问题页面,似乎该错误已在 typescript 的 2.4.0 或 2.4.1 版本中修复。
我检查了我的 package.json 并在“devDependencies”中找到了"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();
}
}
检查这个堆栈闪电战
更新:
好的,在你的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 { }
检查这个工作堆栈闪电战
推荐阅读
- google-cloud-run - 谷歌云运行的准备情况检查 - 如何?
- javascript - 如何正确传递 ReactJS 道具?
- bash - /usr/local/bin/ffmpeg: 无法执行二进制文件
- mysql - 在多个表中将匹配的列与条件放在一起
- sql - 如何为某些员工选择行,但不为其他员工选择行
- javascript - 将 JS 安全 websocket 连接到 C# websocket 服务器 (Fleck)
- mono - 在 Visual Studio Mac 上构建项目时任务找不到“AxImp.exe”
- node.js - 如何删除cookie MEAN堆栈?
- powershell - 从文件名中删除多个连续句点
- javascript - 函数 row.add() 在我的函数中不起作用: