简体   繁体   English

这指向主轴编辑器工具栏

[英]this is pointing to the quill editor toolbar

I use Quilljs for a textarea on my website. 我在网站上将Quilljs用于文本区域。 The standard editor don't support image upload to the server, so i have to implement a custom handler. 标准编辑器不支持将图像上传到服务器,因此我必须实现一个自定义处理程序。 In the documentation is written the following: 在文档中写道

Handler functions will be bound to the toolbar (so using this will refer to the toolbar instance) and passed the value attribute of the input if the corresponding format is inactive, and false otherwise. 处理程序功能将绑定到工具栏(因此使用此功能将引用工具栏实例),并且如果相应格式无效,则传递输入的value属性,否则返回false。

This is actually a problem for me and i dont know how to resolve it in a clean and "right" way. 这实际上对我来说是个问题,我不知道如何以干净和“正确”的方式解决它。 I built an angular application and i have written a custom handler for image uploading. 我建立了一个有角度的应用程序,并为图像上传编写了一个自定义处理程序。 This custom image handler should upload the image with the help of an angular service to the server. 此自定义图像处理程序应在角度服务的帮助下将图像上载到服务器。 The data service is globally provided in the app and a member of my component and i can access it with this.dataService . 数据服务在应用程序和我的组​​件的成员中全局提供,我可以使用this.dataService访问它。 But after clicking the image upload icon in the toolbar, this is bound to the toolbar, i can't access my data service anymore. 但是,单击工具栏中的图片上传图标后, this被绑定到工具栏,我不能再访问我的数据服务。 Can i avoid this boundary to the toolbar? 我可以避免工具栏的边界吗?

In explicit. 在明确。 Assume i have created a quill editor with the following code: 假设我用以下代码创建了一个主轴编辑器:

this.editor = new Quill('#editor', {
      modules: { toolbar: "#toolbar"},
      placeholder: 'Some Placeholder',
      theme: 'snow'
});

Now i add a custom handler to the image icon with 现在我将自定义处理程序添加到图像图标

this.editor.getModule("toolbar").addHandler("image", imageHandler);

and my handler function for instance: 和我的处理函数例如:

imageHandler(event) {
  this.dataService.addImage(event.file);
}

which uses the dataService which i've already implemented and tested. 它使用了我已经实现和测试的dataService。 But this.dataService don't exists because this is now bind to the toolbar. this.dataService不存在,因为this是现在绑定到工具栏。 I initialized the service with the constructor of the component. 我使用组件的构造函数初始化了服务。

constructor(private dataService: DataService) {

}

When i call this.dataService in the constructor, then it can be found and the boundary is fine, but i need to call it in my image handler function to send the file to the server. 当我在构造函数中调用this.dataService时,可以找到它并且边界很好,但是我需要在我的图像处理函数中调用它以将文件发送到服务器。

Best regards, Sven 最好的问候,斯文

The easiest way to solve this problem is to change 解决此问题的最简单方法是更改

this.editor.getModule("toolbar").addHandler("image", imageHandler);

into 进入

this.editor.getModule("toolbar").addHandler("image", imageHandler.bind(this));

now you can access your components variables/members in your image handler function. 现在,您可以在图像处理程序函数中访问组件变量/成员。 The toolbar have not anymore the focus. 工具栏不再具有焦点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM