添加类到 Svelte 组件

huangapple go评论69阅读模式
英文:

Add class to svelte component

问题

Sure, here is the translated code part without additional content:

  1. btn.svelte
<button class="btn"></button>
  1. btnHold.svelte
<script>
    import Btn from './btn.svelte';
</script>

I'm trying to add a new class btn--hold to btn.svelte

<Btn class="{btn} btn--hold"></Btn>

I get an error on class.

Basically I would like in the end to have:

<button class="btn btn--hold"></button>

How can I add a class to an imported component?

英文:

I created two components:

1. btn.svelte

&lt;button class=&quot;btn&quot; /&gt;

2. btnHold.svelte

&lt;script&gt;
	import Btn from &#39;./btn.svelte&#39;;
&lt;/script&gt;

I'm trying to add a new class btn--hold to btn.svelte

&lt;Btn class=&quot;{btn} btn--hold&quot; /&gt;

I get an error on class.

Basically I would like in the end to have:

&lt;button class=&quot;btn btn--hold&quot; /&gt;

How can I add a class to an imported component?

答案1

得分: 5

class 是受保护的关键字,您可以通过像这样更改名称来解决这个问题:

<script>
    // ...
    let className = '';
    export { className as class };
</script>

<button class="btn {className}" ...

用法示例(btn 类已在内部添加):

<Btn class="btn--hold" ...
英文:

class is protected keyword, you can work around that by changing the name like this:

&lt;script&gt;
    // ...
    let className = &#39;&#39;;
    export { className as class };
&lt;/script&gt;

&lt;button class=&quot;btn {className}&quot; ...

Usage example (btn class is already added internally):

&lt;Btn class=&quot;btn--hold&quot; ...

答案2

得分: 1

你可以通过从组件中导出一个名为 className 的变量,并将其添加到按钮的现有类中来实现:

  1. btn.svelte
<script>
    export let className;
</script>

<button class="btn {className}" />
  1. btnHold.svelte
<script>
    import Btn from './btn.svelte';
</script>

<Btn class="btn--hold" />

以将 btn--hold 类添加到按钮上。

英文:

You could do it by exporting a className variable from the component, and adding it onto the button's existing class

  1. btn.svelte
&lt;script&gt;
    export let className;
&lt;/script&gt;

&lt;button class=&quot;btn {className}&quot; /&gt;
  1. btnHold.svelte
&lt;script&gt;
    import Btn from &#39;./btn.svelte&#39;;
&lt;/script&gt;

&lt;Btn class=&quot;btn--hold&quot; /&gt;

to add the btn--hold class to the button.

答案3

得分: 1

btn.svelte组件中,你可以这样做:

<button class={'Btn ' + ($$restProps.class ?? '') } />
  • $$restProps变量是一个包含传递给组件的属性的对象,但这些属性没有通过export let关键字明确声明为props。
  • 我使用了nullish coalescing运算符??,因为如果你没有传递class属性给组件,结果将会是Btn undefined

现在,在btnHold.svelte组件中,你只需这样做:

<Btn class="btn--hold" />

可工作的REPL示例在这里:https://svelte.dev/repl/c2b6625ed73144eeb6bebce6ea4a4d82?version=3.58.0

英文:

In the btn.svelte component you can do:

&lt;button class={ &#39;Btn &#39; + ($$restProps.class ?? &#39;&#39;) } /&gt;
  • The $$restProps variable is an object of attributes which were passed to the component, but not explitly declared as props via the export let keyword.
  • I used the nullish coalescing operator ??, because if you don't pass the class attribute to the component, the result will be Btn undefined.

Now, in the btnHold.svelte component you simply do:

&lt;Btn class=&quot;btn--hold&quot; /&gt;

Working REPL here: https://svelte.dev/repl/c2b6625ed73144eeb6bebce6ea4a4d82?version=3.58.0

huangapple
  • 本文由 发表于 2023年3月31日 16:16:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75896304.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定