【发布时间】:2023-01-17 19:00:51
【问题描述】:
我使用的是 Stencil.js,但语法类似于 React。
const iconSVG = <svg>...</svg>
return (
<button>
{this.icon ?
this.position === 'left'
? iconSVG `${this.label}`
: `${this.label} icon`
: this.label}
</button>
);
这给了我一个错误:iconSVG is not a function
return (
<button>
{this.icon ?
this.position === 'left'
? <svg> ...</svg> `${this.label}`
: `${this.label} icon`
: this.label}
</button>
);
由于this.label,这不起作用。只能有一个元素(值)。
return (
<button>
{this.icon ?
this.position === 'left'
? `${<svg> ...</svg>} `${this.label}`
: `${this.label} icon`
: this.label}
</button>
);
这让我在 label 旁边的 Button 内得到了 [Object object]。
const iconSVG = () => <svg> ...</svg>
return (
<button>
{this.icon ?
this.position === 'left'
? iconSVG() `${this.label}`
: `${this.label} icon`
: this.label}
</button>
);
这给了我一个错误:iconSVG(...) is not a function 显然是因为首先读取了 JSX。
那么,我该怎么做呢?如何在 JSX 中渲染 SVG?
【问题讨论】: