我在as per this Stackoverflow question here下面创建了一个方法映射,以帮助我动态调用静态方法。但是,我始终遇到以下错误:
避免引用可能导致
this
无意范围的未绑定方法。
我该怎么解决这个问题?
addCourseContentElementMethodMap = {
[CourseContentElementMethodMap.ADD_COURSE_CONTENT_ELEMENT_AUDIO]:
CourseContentElementAudioService.addCourseContentElement,
[CourseContentElementMethodMap.ADD_COURSE_CONTENT_ELEMENT_BUTTON]:
CourseContentElementButtonService.addCourseContentElementButton,
[CourseContentElementMethodMap.ADD_COURSE_CONTENT_ELEMENT_CODE_FIDDLE]:
CourseContentElementCodeService.addCourseContentElementCodeFiddle,
[CourseContentElementMethodMap.ADD_COURSE_CONTENT_ELEMENT_CODE_MARKDOWN]:
CourseContentElementCodeService.addCourseContentElementCodeMarkdown,
[CourseContentElementMethodMap.ADD_COURSE_CONTENT_ELEMENT_CODE_REPL]:
CourseContentElementCodeService.addCourseContentElementCodeRepl,
[CourseContentElementMethodMap.ADD_COURSE_CONTENT_ELEMENT_DOCUMENT]:
CourseContentElementDocumentService.addCourseContentElement,
[CourseContentElementMethodMap.ADD_COURSE_CONTENT_ELEMENT_EDITOR]:
CourseContentElementEditorService.addCourseContentElement,
[CourseContentElementMethodMap.ADD_COURSE_CONTENT_ELEMENT_EMBED_TWEET]:
CourseContentElementEmbedService.addCourseContentElementEmbedTweet,
[CourseContentElementMethodMap.ADD_COURSE_CONTENT_ELEMENT_EMBED_YOUTUBE]:
CourseContentElementEmbedService.addCourseContentElementEmbedYoutube,
[CourseContentElementMethodMap.ADD_COURSE_CONTENT_ELEMENT_IMAGE]:
CourseContentElementImageService.addCourseContentElement,
[CourseContentElementMethodMap.ADD_COURSE_CONTENT_ELEMENT_QUIZ]:
CourseContentElementQuizService.addCourseContentElement,
[CourseContentElementMethodMap.ADD_COURSE_CONTENT_ELEMENT_VIDEO]:
CourseContentElementVideoService.addCourseContentElement,
};
这发生在上面的每一行:
55:7 error Avoid referencing unbound methods which may cause unintentional scoping of `this` @typescript-eslint/unbound-method
57:7 error Avoid referencing unbound methods which may cause unintentional scoping of `this` @typescript-eslint/unbound-method
59:7 error Avoid referencing unbound methods which may cause unintentional scoping of `this` @typescript-eslint/unbound-method
61:7 error Avoid referencing unbound methods which may cause unintentional scoping of `this` @typescript-eslint/unbound-method
63:7 error Avoid referencing unbound methods which may cause unintentional scoping of `this` @typescript-eslint/unbound-method
65:7 error Avoid referencing unbound methods which may cause unintentional scoping of `this` @typescript-eslint/unbound-method
67:7 error Avoid referencing unbound methods which may cause unintentional scoping of `this` @typescript-eslint/unbound-method
69:7 error Avoid referencing unbound methods which may cause unintentional scoping of `this` @typescript-eslint/unbound-method
71:7 error Avoid referencing unbound methods which may cause unintentional scoping of `this` @typescript-eslint/unbound-method
73:7 error Avoid referencing unbound methods which may cause unintentional scoping of `this` @typescript-eslint/unbound-method
75:7 error Avoid referencing unbound methods which may cause unintentional scoping of `this` @typescript-eslint/unbound-method
77:7 error Avoid referencing unbound methods which may cause unintentional scoping of `this` @typescript-eslint/unbound-method
有关的方法如下:
export class CourseContentElementAudioService {
constructor(private courseContentElementService: CourseContentElementsService) {}
}
其中一种方法的示例:
static addCourseContentElement(
courseContents: ICourseContent[],
selectedCourseContentElementUid: string,
selectedCourseContentUid: string | undefined
): ICourseContent[] {
return CourseContentElementsService.addCourseContentElement(
courseContents,
selectedCourseContentUid,
{
uid: selectedCourseContentElementUid,
url: initialState.courseMedia.audio[0].url,
},
CourseContentElementType.AUDIO
);
}
发布于 2021-05-04 21:59:40
有一些选项可供您选择:
关闭了这个林特规则
的林特规则
。
[CourseContentElementMethodMap.ADD_COURSE_CONTENT_ELEMENT_AUDIO]:
CourseContentElementEditorService.addCourseContentElement.bind(CourseContentElementEditorService)
[CourseContentElementMethodMap.ADD_COURSE_CONTENT_ELEMENT_AUDIO]:
(...args) => CourseContentElementEditorService.addCourseContentElement(...args)
发布于 2022-10-11 15:13:45
将选项添加到配置中
"@typescript-eslint/unbound-method": ["error", {
"ignoreStatic": true
}],
https://stackoverflow.com/questions/67392626
复制相似问题