`this.Get(“object. Value”)` 在 Ember.js 中无法正常工作。

huangapple go评论67阅读模式
英文:

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);
}

huangapple
  • 本文由 发表于 2023年2月24日 14:22:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75553205.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定