我遇到了一些界面问题,当我尝试使用我的函数时,控制台抛出错误:error_handler.js:55 EXCEPTION:包中出错:355:100:20导致:无法读取属性& #39; registerStudent'未定义的 这是我的界面:
export interface StorageInterface{
registerStudent(obj: StudentRegistrationDTO);}
ApiStorage:
import { StorageInterface } from './storage.interface';
export class ApiStorage implements StorageInterface{
registerStudent(obj){
console.log('after');
}
}
我的DTO:
import { ApiStorage} from '../api.storage';
export class StudentRegistrationDTO{
private apiStorage: ApiStorage;
constructor(
private firstName: string
){}
registerStudentDTO(){
console.log('before');
this.apiStorage.registerStudent(this);
}
和我的组件文件:
import { StudentRegistrationDTO } from '../storage/dto/registration.student.dto';
export class StudentRegisterComponent {
sRDTO: StudentRegistrationDTO;
send(){
this.sRDTO = new StudentRegistrationDTO(
this.service.first_name)
this.sRDTO.registerStudentDTO();
}
}
你知道也许这里发生了什么吗?为什么会抛出错误? 控制台打印"之前"然后抛出错误。
答案 0 :(得分:1)
这是因为你没有实例化你的apiStorage
变量。试试这个DTO:
import { ApiStorage} from '../api.storage';
export class StudentRegistrationDTO{
private apiStorage: ApiStorage;
constructor(
private firstName: string
){
this.apiStorage = new ApiStorage(/* any object that suits your constructor */);
}
registerStudentDTO(){
console.log('before');
this.apiStorage.registerStudent(this);
}