[英]Property 'users_index' does not exist on type 'typeof UserApiController' when trying to access static method in TypeScript
I am brand new to TypeScript, and trying to call a function on a class.我是 TypeScript 的新手,并试图在类上调用函数。
In file A:在文件 A 中:
import userAPIController from "./controllers/customer/userAPIController";
userAPIController.users_index();
In file B:在文件 B 中:
export default class UserApiController {
public users_index(): void {
User.findAll({
attributes: ["given_name", "family_name"]
}).success(function (users: any) {
console.log(users);
});
}
I get the error:我收到错误:
Property 'users_index' does not exist on type 'typeof UserApiController'
I've also tried to add an interface:我也尝试添加一个接口:
interface userAPIController {
users_index: any;
}
With no luck.没有运气。
What am I doing incorrectly?我做错了什么?
UsersApiController
is a class, so you'll need to instantiate it before you can call its methods: UsersApiController
是一个类,因此您需要先实例化它,然后才能调用其方法:
const controller = new usersApiController();
controller.users_index();
You can read more about using classes here: https://www.typescriptlang.org/docs/handbook/classes.html您可以在此处阅读有关使用类的更多信息: https : //www.typescriptlang.org/docs/handbook/classes.html
The function you've written isn't a isn't a static method, it's an instance method.您编写的函数不是静态方法,而是实例方法。 If you want it to be static, change it to:如果您希望它是静态的,请将其更改为:
static users_index = (): void => {
// content omitted
}
For more on static properties, see this page有关静态属性的更多信息, 请参阅此页面
Or if it's correct that it's an instance method, use the new
operator to create an instance before you try to access it:或者,如果它是一个实例方法是正确的,请在尝试访问它之前使用new
运算符创建一个实例:
const controller = new UserApiController();
controller.users_index();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.