英文:
this. Get( "object. Value") not working properly in ember.js
问题
有时在ember.js中,this.get("object.value")
无法正常工作。但this.get("object").value
可以正常工作。有什么原因吗?
this.get("object").value = "values" // 正常工作 ✅
this.get("object.value") = undefined // 不起作用
英文:
Sometimes this.get("object.value") not working properly in ember.js. But this.get("object").value is working. Any reason?
this.get("object").value = "values" // WORKS ✅
this.get("object.value") = undefined // DOESN'T WORK
答案1
得分: 3
能澄清一下"什么不起作用"吗?您期望发生什么?您的数据是什么样的?
object.value
是不是意外地带有一个点的键?
例如:
{
'object1.value': 1,
// vs
object2: {
value: 2
}
}
使用 object1.value
不会起作用,因为没有名为object1
的键
但 object2.value
会起作用,因为有一个名为object2
的键
因此,如果您想访问带有键中包含.
的属性,您需要编写一个自定义方法来实现(可能使用方括号访问)。
例如:
import { get } from '@ember/object';
function customGet(obj, keyOrPropertyPath) {
if (keyOrPropertyPath in obj) return obj[keyOrPropertyPath];
return get(obj, keyOrPropertyPath);
}
英文:
Can you clarify "what isn't working"? what do you expect to happen? what does your data look like?
is object.value
accidentally a key with a dot in it?
For example:
{
'object1.value': 1,
// vs
object2: {
value: 2
}
}
get with object1.value
won't work because there is no key named object1
But object2.value
will work because there is a key named object2
so, if you want to access properties with .
in the key, you'll need a custom method that does that (likely with square bracket access).
for example:
import { get } from '@ember/object';
function customGet(obj, keyOrPropertyPath) {
if (keyOrPropertyPath in obj) return obj[keyOrPropertyPath];
return get(obj, keyOrPropertyPath);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论