简体   繁体   English

Ractive.js带有破折号的变量

[英]Ractive.js variable with dash

Back end (WordPress) generates JSON output: 后端(WordPress)生成JSON输出:

sizes: {
thumbnail: "150x150.jpg",
thumbnail-width: 150,
thumbnail-height: 150,
}

In JS I can use variable with a dash that way: sizes['thumbnail-height'] . 在JS中,我可以使用带有破折号的变量: sizes['thumbnail-height'] Unfortunately, in Ractive.js template {{sizes['thumbnail-height']}} does not work. 不幸的是,在Ractive.js中,模板{{sizes['thumbnail-height']}}不起作用。 How I can deal with this issue? 我该如何处理这个问题?

You can write the objects keys as string then you can use the - sign. 您可以将对象键写为字符串,然后可以使用-符号。 That said is a good practice to avoid it and use camelCase style. 那是避免它并使用camelCase样式的一个好习惯。 If you realize for css attributes are always translated into camel case. 如果您认识到css属性,则始终将其转换为驼峰式大小写。

sizes: {
    'thumbnail': "150x150.jpg",
    'thumbnail-width': 150,
    'thumbnail-height': 150,
}
// Best
    sizes: {
        thumbnail: "150x150.jpg",
        thumbnailWidth: 150,
        thumbnailHeight: 150,
    }

The first case still works fine with Ractive (see http://jsfiddle.net/6hd3xt6t/ ) using the quoted property names like {{sizes['thumbnail-height']}} 第一种情况在Ractive上仍然可以正常使用(请参见http://jsfiddle.net/6hd3xt6t/ ),并使用诸如{{sizes['thumbnail-height']}}等带引号的属性名称

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

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