[英]Can't access method from the model class in Angular 4
I am trying to use method from the model class but its throwing error like 我试图使用模型类中的方法,但它的抛出错误就像
ERROR TypeError: act.sayHi is not a function
错误TypeError:act.sayHi不是函数
Here is my code 这是我的代码
Model Class myModelClass.ts
模型类myModelClass.ts
export interface IMymodel {
name: string;
address: string;
age: number;
}
export class Mymodel implements IMymodel {
name: string;
address: string;
age: number
constructor() {}
sayHi(name): string {
console.log('Hiii' + name);
return 'Hiii'+name;
}
}
Component MyComponent.ts
组件MyComponent.ts
import { IMymodel, Mymodel } from '../model/myModelClass.ts';
@Component({
selector: 'my-component',
templateUrl: 'myComponent.html'
})
export class MyComponent {
...
prepareData(data: Array<IMymodel>): Array<IMymodel> {
data.map((act: Mymodel) => {
act.sayHi(act.name);
});
return data;
}
}
You need to put the sayHi
method signature into the interface too as you access your objects via interface type and that type lacks a method with name sayHi
. 当您通过接口类型访问对象时,您需要将
sayHi
方法签名放入接口,并且该类型缺少名为sayHi
的方法。
export interface IMymodel {
name: string;
address: string;
age: number;
sayHi(name): string
}
and method call 和方法调用
prepareData(data: Array<IMymodel>): Array<IMymodel> {
data.map((act: IMymodel) => {
act.sayHi(act.name);
});
return data;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.