![](/img/trans.png)
[英]Property 'noise' does not exist on type 'typeof @types/p5/index"'
[英]Property 'users_index' does not exist on type 'typeof UserApiController' when trying to access static method in TypeScript
我是 TypeScript 的新手,並試圖在類上調用函數。
在文件 A 中:
import userAPIController from "./controllers/customer/userAPIController";
userAPIController.users_index();
在文件 B 中:
export default class UserApiController {
public users_index(): void {
User.findAll({
attributes: ["given_name", "family_name"]
}).success(function (users: any) {
console.log(users);
});
}
我收到錯誤:
Property 'users_index' does not exist on type 'typeof UserApiController'
我也嘗試添加一個接口:
interface userAPIController {
users_index: any;
}
沒有運氣。
我做錯了什么?
UsersApiController
是一個類,因此您需要先實例化它,然后才能調用其方法:
const controller = new usersApiController();
controller.users_index();
您可以在此處閱讀有關使用類的更多信息: https : //www.typescriptlang.org/docs/handbook/classes.html
您編寫的函數不是靜態方法,而是實例方法。 如果您希望它是靜態的,請將其更改為:
static users_index = (): void => {
// content omitted
}
有關靜態屬性的更多信息, 請參閱此頁面
或者,如果它是一個實例方法是正確的,請在嘗試訪問它之前使用new
運算符創建一個實例:
const controller = new UserApiController();
controller.users_index();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.