英文:
Define width and background-color of a div with StencilJS
问题
<div class="SomeClass" style="width: array[0].value; background-color: array[0].color">
render() {
return (
this.objArray.forEach(element => {
if (element.color == undefined || element.color == null || element.color == '') {
element.color = this.colors[this.index];
this.index = this.index == (this.colors.length - 1) ? 0 : (this.index += 1);
}
if (element.value == null || element.value == 0) {
element.value = 100;
}
if (element.tooltip == null) {
element.tooltip = '';
}
return `<div class style="width:${element.value}; background-color:${element.color}"></div>`;
});
)
}
I have removed the unnecessary HTML encoding (<
and >
) from the code snippets.
英文:
I have an array with some properties (values are variable).
[{"color": "#2c8646",
"value": 30
},
{"color": "#69c0f9",
"value": 50 }]
And then, I need to return divs with that, something like:
render(){
return (
<div class="SomeClass" style="width: array[0].value ; background-color: array[0].color" >
)
The matter is that style in StencilJS doesn't work like that. I can't use a static css file due that the Array is different any time.
Any idea?
TIA
I need to do something like
this.objArray.forEach(element => {
if(element.color == undefined || element.color == null || element.color == '')
{
element.color = this.colors[this.index];
this.index = this.index == (this.colors.length - 1) ? 0 : (this.index += 1 );
}
if(element.value == null || element.value == 0 )
element.value = 100;
if(element.tooltip == null )
element.tooltip = '';
return <div class style={`width:${element.value}; background-color ${element.color}`> </div>;
});
答案1
得分: 1
Stencil 允许样式具有动态性 - 我们可以从一个简单的示例开始,看起来像这样:
import { Component, h } from '@stencil/core';
@Component({
tag: 'my-component',
shadow: true,
})
export class MyComponent {
render() {
return <div style={{ background: "#ffe7e8", border: "2px solid #e66465" }}>Hello, World! I'm Stencil</div>;
}
}
在这里,style
属性的内容包含两对花括号 - 一对用于表示我们有一个对象字面量:
{ background: "#ffe7e8", border: "2px solid #e66465" }
以及包围该对象字面量的外部一对,告诉 Stencil 插值/读取这个值。
然后,我们可以添加一个样式数组,并使用 map()
函数来为每个样式显示一个 div
:
import { Component, h } from '@stencil/core';
type Styles = Record<string, string>[];
const myStyles: Styles = [
{ background: "#ffe7e8", border: "2px solid #e66465" },
{ background: "#26b72b", border: "1px dashed blue" }
]
@Component({
tag: 'my-component',
shadow: true,
})
export class MyComponent {
render() {
return (<div>{myStyles.map((myStyle) => <div style={myStyle}>Hello, World! I'm Stencil</div>)}</div>);
}
}
请注意:
Styles
只是一个包含字符串到字符串映射的数组。myStyles
包含两种不同的样式供我们迭代。- 我们在
render
函数中使用map()
- 这是必要的,以便为myStyles
中的每个条目返回一个新的div
。 - 我们只使用一对花括号,因为
render
函数中的myStyle
已经是一个对象。
使用 map
是重要的,因为 forEach
不会为每次迭代返回一个新值。如果我将我的 render
函数更改为:
render() {
return (<div>{myStyles.forEach((myStyle) => <div style={myStyle}>Hello, World! I'm Stencil</div>)}</div>);
}
则什么都不会渲染。
要使您的 forEach
可行,我建议进行以下更改:
- 将
forEach
更改为map
。 - 更新样式生成逻辑,以构建一个对象字面量,而不是一个字符串,像这样:
const newStyle = {
width: element.value,
backgroundColor: element.color
};
return <div style={newStyle}> </div>;
英文:
Stencil allows for styles to be dynamic - a simple example that we can build on would look somethings like this:
import { Component, h } from '@stencil/core';
@Component({
tag: 'my-component',
shadow: true,
})
export class MyComponent {
render() {
return <div style={{ background: "#ffe7e8", border: "2px solid #e66465" }}>Hello, World! I'm Stencil</div>;
}
}
where we just output "Hello World, I'm Stencil!" in a red bordered and pink-ish background.
Note that here, the contents of the style
attribute has two curly braces - one to denote that we have an object literal:
{ background: "#ffe7e8", border: "2px solid #e66465" }
and an outer pair surrounding that object literal to tell Stencil to interpolate/read this value.
We can then add an array of styles, and use the map()
function in order to show a div
for each style:
import { Component, h } from '@stencil/core';
type Styles = Record<string, string>[];
const myStyles: Styles = [
{ background: "#ffe7e8", border: "2px solid #e66465" },
{ background: "#26b72b", border: "1px dashed blue" }
]
@Component({
tag: 'my-component',
shadow: true,
})
export class MyComponent {
render() {
return (<div>{myStyles.map((myStyle) => <div style={myStyle}>Hello, World! I'm Stencil</div>)}</div>);
}
}
Note that:
Styles
is just an array containing a mapping of strings to stringsmyStyles
contains two different styles for us to iterate over- We use
map()
in therender
function - this is necessary in order to have a newdiv
returned for each and every entry inmyStyles
. - We use only one set of curly braces, as
myStyle
in therender
function is already an object
Using map
is important, as forEach
does not return a new value for each iteration. If I were to change my render
function to:
render() {
return (<div>{myStyles.forEach((myStyle) => <div style={myStyle}>Hello, World! I'm Stencil</div>)}</div>);
}
Nothing would render.
To make your forEach
workable, I'd suggest changing:
forEach
to amap
- updating the style generation logic to build an object literal instead of a string like so:
const newStyle = {
width: element.value,
background-color: element.color
};
return <div class style={newStyle}> </div>;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论