【发布时间】:2021-07-27 06:20:00
【问题描述】:
我在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
);
}
【问题讨论】:
-
您保存的方法似乎不是静态的。如果它们是实例方法,则需要在
this实例上使用 lambda 调用它们。 -
我很确定它们是静态方法。我已经发布了一个示例。
标签: angular typescript eslint