我正在尝试使用带有一些添加插件的 Redactor。问题是计数器插件在页面加载后显示 0 个单词和 0 个字符。
{
words: 0,
characters: 0,
spaces: 0
}
我尝试使用"init"回调来执行计数器插件的count()方法,如文档所示:
$('#content').redactor({
plugins: ['counter'],
callbacks: {
init: function()
{
this.counter.count();
},
counter: function(data)
{
console.log(data);
}
}
});
。在这一点上,一切似乎都很好,VSCode 中没有编译错误,但我在控制台中收到以下错误:
declare const $R: any;
...
...
$R('#editor', {
plugins: [
'counter',
...
],
callbacks: {
init: function() {
this.counter.count();
}
counter: function(data: any) {
console.log(data);
}
}
});
ERROR TypeError: Cannot read property 'count' of undefined
at App.changed (editor.component.ts:55)
at F._loop (scripts.bundle.js:2741)
at F.trigger (scripts.bundle.js:2711)
at App.broadcast (scripts.bundle.js:2185)
at F._syncing (scripts.bundle.js:10344)
at scripts.bundle.js:10316
at ZoneDelegate.invokeTask (zone.js:421)
at Object.onInvokeTask (core.js:4724)
at ZoneDelegate.invokeTask (zone.js:420)
at Zone.runTask (zone.js:188)
我做错了什么?
谢谢
在this.counter
中,this
指的是init
中的函数,而不是全局类。 请改用箭头函数:
callbacks: {
init: () => {
this.counter.count();
}
counter: (data: any) => {
console.log(data);
}
}
这仅适用于在编校器上下文之外引用组件类counter
的情况。
最后我找到了解决方案:
declare const $R: any;
...
...
$R('#editor', {
plugins: [
'counter',
...
],
callbacks: {
syncing: function(html: any) {
this.plugin.counter.count();
},
counter: function(data: any) {
console.log(data);
}
}
});
必须添加"插件"来引用我想要达到的插件。