我有一个Typescript类,其中的方法无法识别



我有一个Typesript类AjaxBookStringParams。当我尝试使用函数/方法BuildGenieParams时,错误是:;BookGenieLoader.ts:47未捕获类型错误:ajaxString.BuildGenieParams不是函数。我一定在做一件非常简单的事情,但这件事不起作用。注意:它是我的任何类中唯一的方法。

这是课程:

export class AjaxBookStringParams {
AgeCandidates: string = ""; // AllocationCandidates by age to-from server
BookToDelete:  string = ""; // Used to delete an allocated book
AllocatedBooks:  string = "";; // placeholder for to-from server - grid display
BookGenieParams: string = "";
Data: string = "";
ProductFilterParams:  string = "";
Request: string = "";
constructor(ajaxParams:AjaxBookGenieParams){
this.AgeCandidates = JSON.stringify(ajaxParams.AgeCandidates);
this.BookToDelete = JSON.stringify(ajaxParams.AgeCandidates);
this.AllocatedBooks = JSON.stringify(ajaxParams.AllocatedBooks);
this.BookGenieParams = JSON.stringify(ajaxParams.BookGenieParams);
this.ProductFilterParams = JSON.stringify(ajaxParams.ProductFilterParams);
this.Data = ajaxParams.Data;
this.Request = ajaxParams.Request;
}
BuildGenieParams():AjaxBookGenieParams{
const ajaxBookGenieParams:AjaxBookGenieParams  = new  AjaxBookGenieParams();
if(this.AgeCandidates != ""){ajaxBookGenieParams.AgeCandidates = JSON.parse(this.AgeCandidates)}
if(this.BookToDelete != ""){ajaxBookGenieParams.BookToDelete = JSON.parse(this.BookToDelete)}
if(this.AllocatedBooks != ""){ajaxBookGenieParams.AllocatedBooks = JSON.parse(this.AllocatedBooks)}
if(this.BookGenieParams != ""){ajaxBookGenieParams.BookGenieParams = JSON.parse(this.BookGenieParams)}
if(this.ProductFilterParams != ""){ajaxBookGenieParams.ProductFilterParams = JSON.parse(this.BookGenieParams)}  
ajaxBookGenieParams.Data = this.Data;
ajaxBookGenieParams.Request = this.Request;
return ajaxBookGenieParams;
}
getJsonString():string{
const jsonString = JSON.stringify({ajaxBookStringParams: AjaxBookStringParams,}) ;
return jsonString;
}}

它被称为:

$(function () {
// this is param load from initial page load - only used once
let element = document.getElementById("AjaxBookGenieParamsHold");
let htmlElement: HTMLElement = element as HTMLElement;
let tParam: string = htmlElement.innerHTML;
let ajaxString:AjaxBookStringParams = JSON.parse (tParam);
**let ajaxBookGenieParams:AjaxBookGenieParams = ajaxString.BuildGenieParams();**
BookGenieMethods.ajaxBookGenieParams = ajaxBookGenieParams; // initial load of values

参考类别为:

export class AjaxBookGenieParams {
AgeCandidates: AgeCandidate[] = []; // AllocationCandidates by age to-from server
BookToDelete: Book = new Book(); // Used to delete an allocated book
AllocatedBooks: AllocatedBook[] = []; // placeholder for to-from server - grid display
BookGenieParams: BookGenieParams = new BookGenieParams();
Data: string = "";
ProductFilterParams: ProductFilterParams = new ProductFilterParams();
Request: string = "";}
export class ProductFilterParams {
RootNodes: RootNode[] = [];
FilterGuid: string = "";
AgeRange: AgeRange = new AgeRange();
SelectedAges: number[] = [];
ResetFilter: string = "";}
export class AgeRange {
Min: number = 0;
Max: number = 12;
From: number = 0;
FromInit: number = 0;
To: number = 0;
ToInit: number = 0;}
export class RootNode {
Description: string = "";
GroupType: string = "";
Id: number = 0;
Name: string = "";
NodeCategories: NodeCategory[] = [];
Selected: string = "";
ToolTip: string = "";
__expanded__: boolean = false;}
export class NodeCategory {
Id: number = 0;
Name: string = "";
Count: number = 0;
Selected: string = "";
Description: string = "";}
export class BookGenieParams {
AllocatedQuantity: number = 0;
AllocatedSubTotal: number = 0;
AllocatedTitles: number = 0;
AllocationBudget: number = 200;
BooksPerTitle: number = 5;
CustomerGuid: string = "";
CustomerId: number = 0;
DefaultBooksPerTitle: number = 5;
MinimumBudget: number = 200;
MoveToCartSubTotal: number = 0;
MoveToCartCount: number = 0;
ReDisplayBreak: number = 10;
SelectedCategories: number[] = [];  // Calculated from PFP
UserBooksPerTitle: number = 0;}
export class AllocatedBook {
AgeBooks: Book[] = [];
AgeId: number = 0;
Name: string = "";
Titles: number = 0;
Quantity: number = 0;
SubTotal: number = 0}
export class Book {
ISBN: string = "";
ProductId: number = 0;
ImageUrl: string = "";
Title: string = "";
Price: number = 0;
Quantity: number = 0;
AgeId: number = 0}
export class AgeCandidate {
AgeId: number = 0;
AllocationCandidates: AllocationCandidate[] = [];}
export class AllocationCandidate {
AgeId: number = 0;
DateLastTouched: Date = new Date;
Inventory: number = 0;
ISBN: string = "";
Price: number = 0;
ProdctId: number = 0;
Title: string = "";
}

JSON.parse返回any,并将其结果分配给类型为AjaxBookStringParams的变量。编译器不会警告您。

由于您只得到一个对象,而没有一个AjaxBookStringParams实例,因此您将在运行时看到错误。

您需要根据parse结果创建一个真正的AjaxBookStringParams实例。

它可能是这样的:

const ajaxString = new AjaxBookStringParams(JSON.parse (tParam));

最新更新