[英]Getting uncaught error in ember
When I'm doing the right click option for more than 5 (approx) times for certain task, it showing uncaught error as like below: 当我对某些任务执行右键单击选项超过5次(大约)时,它显示未捕获的错误,如下所示:
Uncaught TypeError: Cannot read property 'find' of undefined
at Class.<anonymous> (core.js:21487)
at fn (core.js:7779)
at DeferredActionQueues.flush (core.js:7723)
at Backburner.end (core.js:7738)
at Backburner.run (core.js:7748)
at executeTimers (core.js:7824)
at core.js:7822
In that Place I'm having the below code: 在那个地方,我有以下代码:
Ember.run.later(view, function () {
this.$().find('menu-item:eq(0)').focus();
}, 125);
Can anyone please suggest me why this error is coming and I need to avoid this error while right clicking the task for "n" number of time also. 任何人都可以建议我为什么会出现此错误,并且我还需要在右键单击任务“ n”次的同时避免此错误。 I'm new to the ember.
我是炭烬新手。 Your help will be appreciate.
您的帮助将不胜感激。 Thanks in advance.
提前致谢。
Thats a simple javascript issue. 多数民众赞成在一个简单的JavaScript问题。 In the second line
this.$()
returns undefined
, and so it can't call .find
on undefined. 在第二行中,
this.$()
返回undefined
,因此无法在undefined上调用.find
。
More interesting is why this.$()
is undefined. 更有趣的是为什么
this.$()
未定义。 Probably you have this code inside a component, and try to access the local jQuery
instance . 可能您的代码在组件内部,然后尝试访问本地
jQuery
实例 。 However you call it inside an anonymous function(){}
, which breaks your this
-context (because it gets a new one). 但是你把它叫做一个匿名内部
function(){}
打破你的this
-context(因为它获得一个新的)。
The best solution here is to use an arrow function: 最好的解决方案是使用箭头功能:
Ember.run.later(view, () => {
this.$().find('menu-item:eq(0)').focus();
}, 125);
this prevents the outer this
context, which is nice. 这样可以防止外部
this
上下文,这很好。 Another options is to save this: 另一种选择是保存此:
const self = this;
Ember.run.later(view, function () {
self.$().find('menu-item:eq(0)').focus();
}, 125);
Or you can .bind(this)
: 或者你可以
.bind(this)
:
Ember.run.later(view, (function () {
this.$().find('menu-item:eq(0)').focus();
}).bind(this), 125);
I can definitely recommend the first option, especially when using ember(-cli) which gives you transpilation anyway. 我绝对可以推荐第一种选择,尤其是在使用ember(-cli)时,无论如何它都能使您进行转译。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.