简体   繁体   English

Ember 模板助手 get-value-with-key

[英]Ember template helper get-value-with-key

Ember 是否有任何模板助手“get-value-with-key”我发现了以下用法,但不确定它到底是做什么的?

{{get-value-with-key item optionValuePath}}

There is an Ember Get Helper for HTMLBars. HTMLBars 有一个 Ember Get Helper。 You might have to install the Package "ember-get-helper" if you are on ember < 2.1.如果您使用的是 ember < 2.1,则可能需要安装包“ember-get-helper”。

{{get object key}}

Let's say you have the following object:假设您有以下对象:

var obj = {
  "key1": {
    "subkey1": "hello world"
  }
}

Using Ember 3.18, to access "hello world" from the template, you can do:使用 Ember 3.18,要从模板访问“hello world”,您可以执行以下操作:

{{get obj 'key1.subkey1'}}

You can use the build-in get helper.您可以使用内置的 get 助手。 See docs here: Ember Docs .请参阅此处的文档: Ember Docs

Usage example:用法示例:

{{get object key}}

Note that the get helper will not be able to work on all JavaScript keys.请注意,get 助手将无法处理所有 JavaScript 键。 For example, a key with a '.'例如,带有“.”的键will not work with the built-in get helper.不适用于内置的 get 助手。

For example, if you have a valid JavaScript object like:例如,如果您有一个有效的 JavaScript 对象,例如:

const example = {
    'example.pdf': 'pdf_url'
}

// You can access this key normally via
example['example.pdf']

however, this will not work in the get helper但是,这在 get 助手中不起作用

{{get this.example 'example.pdf'}}

One solution is to create a helper that can support the types of keys you need to support.一种解决方案是创建一个可以支持您需要支持的密钥类型的助手。 For example, I made a helper that can work on keys with a '.'例如,我制作了一个可以处理带有 '.' 的键的助手。 by including '.'通过包括“。” in the key name which are escaped like with ''.在键名中像''一样转义。

{{get this.example 'example\.pdf'}}

The ember twiddle can be found here: twiddle ember twiddle 可以在这里找到: twiddle

Other Helpful Sources:其他有用的来源:

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

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