Polymer 中如何处理可以是String 或Object 的属性?
How to deal with a property which can be a String or an Object in Polymer?
我正在尝试使用 mapbox-gl-js 创建一个聚合物网络组件。
我想在 Polymer 自定义元素上使用 属性 设置圆形颜色 属性 (https://www.mapbox.com/mapbox-gl-js/style-spec/#paint-circle-color),但是这个 属性 可以是字符串或对象,请参阅:
"circle-color": "#ff0"
或者在数据驱动的情况下属性
"circle-color": {
"property": "temperature",
"stops": [
// "temperature" is 0 -> circle color will be blue
[0, 'blue'],
// "temperature" is 100 -> circle color will be red
[100, 'red']
]
}
那么我应该在 Polymer 组件的属性函数中使用什么?字符串还是对象?
class XCustom extends Polymer.Element {
static get properties() {
return {
circle-color: String,
// or circle-color: Object ?
}
}
}
customElements.define('x-custom', XCustom);
感谢您的帮助! (而且我已经检查过 https://www.webcomponents.org/element/PolymerVis/mapbox-gl,它没有完成这项工作)
没关系。您可以同时存储两者,但为了清楚起见,将其存储为对象,如果变量是字符串,则将其存储为对象。
我正在尝试使用 mapbox-gl-js 创建一个聚合物网络组件。
我想在 Polymer 自定义元素上使用 属性 设置圆形颜色 属性 (https://www.mapbox.com/mapbox-gl-js/style-spec/#paint-circle-color),但是这个 属性 可以是字符串或对象,请参阅:
"circle-color": "#ff0"
或者在数据驱动的情况下属性
"circle-color": {
"property": "temperature",
"stops": [
// "temperature" is 0 -> circle color will be blue
[0, 'blue'],
// "temperature" is 100 -> circle color will be red
[100, 'red']
]
}
那么我应该在 Polymer 组件的属性函数中使用什么?字符串还是对象?
class XCustom extends Polymer.Element {
static get properties() {
return {
circle-color: String,
// or circle-color: Object ?
}
}
}
customElements.define('x-custom', XCustom);
感谢您的帮助! (而且我已经检查过 https://www.webcomponents.org/element/PolymerVis/mapbox-gl,它没有完成这项工作)
没关系。您可以同时存储两者,但为了清楚起见,将其存储为对象,如果变量是字符串,则将其存储为对象。