【发布时间】:2020-02-06 17:05:50
【问题描述】:
我有一个名为ParentService 的abstract class 及其child class ChildService 如下:
ParentService
import { Injectable } from '@angular/core';
import { MyModel} from './mymodel-model.service';
@Injectable({
providedIn: 'root'
})
export abstract class ParentService {
constructor() { }
word:MyModel = {
"AA":"AA",
"BB":"BB",
}
}
ChildService
import { Injectable } from '@angular/core';
import { MyModel} from './mymodel-model.service';
import { ParentService } from './parent.service';
@Injectable({
providedIn: 'root'
})
export class ChildService extends ParentService {
word2:MyModel = {
"AA":"AA2",
"BB":"BB2",
};
}
在app-component 的constructor 中我声明了子实例如下:
constructor(private child_instance:ChildService){}
在浏览器控制台中打印 child_instance 时,我得到:
ParentService {word: {…}}
没有extends ParentService 我得到了:
ChildService {word2: {…}}
但我需要将两个变量放在同一个类中:
ChildService {word: {…},word2: {…}}
//or
ParentService {word: {…},word2: {…}}
我怎样才能做到这一点?
【问题讨论】:
-
构造函数中不能有child和parent两个参数吗?
标签: javascript angular typescript angular6