循环一个数组以用于动态作用域插槽

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

Loop an Array for Dynamic Scoped Slots

问题

<template>
  <q-btn-toggle v-model="encryptionType" spread class="q-mt-lg" no-caps unelevated :toggle-color="toggleColor" :toggle-text-color="toggleTextColor" :color="color" :text-color="textColor" @update:model-value="checkButtonToggle" :options="options">
    <div v-if="options.length > 1">
      <div v-for="type in options" :key="type.value">
        <template #[type.value]> <!-- 似乎这部分工作不正常 -->
          <q-icon v-if="props[type.value] && encryptionType === type.value" name="check_circle" color="white" class="q-ml-sm" />
        </template>
      </div>
    </div>
  </q-btn-toggle>
</template>
options: [
  { label: 'rsa', value: 'rsa', attrs: { class: 'text-weight-bold font-size-18' }, slot: 'rsa' },
  { label: 'des', value: 'des', attrs: { class: 'text-weight-bold font-size-18' }, slot: 'des' },
  { label: 'aes', value: 'aes', attrs: { class: 'text-weight-bold font-size-18' }, slot: 'aes' }
]
<div v-for="type of options" :key="type.value">
  <template :v-slot="type.value">
    <q-icon v-if="props[type.value] && encryptionType === type.value" name="check_circle" color="white" class="q-ml-sm" />
  </template>
</div>

想要更简洁的方式来实现,因为现在这种方式实在是太重复了:

<template #des>
  <q-icon  v-if="props.des && encryptionType === 'des'" name="check_circle" color="white" class="q-ml-sm" />
</template>

<template #aes>
  <q-icon  v-if="props.aes && encryptionType === 'aes'" name="check_circle" color="white" class="q-ml-sm" />
</template>

<template #rsa>
  <q-icon v-if="props.rsa && encryptionType === 'rsa'" name="check_circle" color="white" class="q-ml-sm" />
</template>

<details>
<summary>英文:</summary>

I am trying to simplify some codes and wanted to loop through an array, from which the scope names will be taken from:

<template>
<q-btn-toggle v-model="encryptionType" spread class="q-mt-lg" no-caps unelevated :toggle-color="toggleColor" :toggle-text-color="toggleTextColor" :color="color" :text-color="textColor" @update:model-value="checkButtonToggle" :options="options">
<div v-if="options.length > 1">
<div v-for="type in options" :key="type.value">
<template #[type.value]> <!-- seems like this part isnt working correctly -->
<q-icon v-if="props[type.value] && encryptionType === type.value" name="check_circle" color="white" class="q-ml-sm" />
</template>
</div>
</div>
</q-btn-toggle>
</template>

Then the options is like this:

```js
options: [
  { label: &#39;rsa&#39;, value: &#39;rsa&#39;, attrs: { class: &#39;text-weight-bold font-size-18&#39; }, slot: &#39;rsa&#39; }
  { label: &#39;des&#39;, value: &#39;des&#39;, attrs: { class: &#39;text-weight-bold font-size-18&#39; }, slot: &#39;des&#39; }
  { label: &#39;aes&#39;, value: &#39;aes&#39;, attrs: { class: &#39;text-weight-bold font-size-18&#39; }, slot: &#39;aes&#39; }
]

However I am getting this error message:

Codegen node is missing for element/if/for node. Apply appropriate transforms first.

If I change how I use scoped slots to this, then there's no error, but <q-icon> isnt rendered as well.

      &lt;div v-for=&quot;type of options&quot; :key=&quot;type.value&quot;&gt;
        &lt;template :v-slot=&quot;type.value&quot;&gt;
          &lt;q-icon v-if=&quot;props[type.value] &amp;&amp; encryptionType === type.value&quot; name=&quot;check_circle&quot; color=&quot;white&quot; class=&quot;q-ml-sm&quot; /&gt;
        &lt;/template&gt;
      &lt;/div&gt;

Want a cleaner way to do it since doing this is just plain too repetitive:

&lt;template #des&gt;
  &lt;q-icon  v-if=&quot;props.des &amp;&amp; encryptionType === &#39;des&#39;&quot; name=&quot;check_circle&quot; color=&quot;white&quot; class=&quot;q-ml-sm&quot; /&gt;
&lt;/template&gt;

&lt;template #aes&gt;
  &lt;q-icon  v-if=&quot;props.aes &amp;&amp; encryptionType === &#39;aes&#39;&quot; name=&quot;check_circle&quot; color=&quot;white&quot; class=&quot;q-ml-sm&quot; /&gt;
&lt;/template&gt;

&lt;template #rsa&gt;
  &lt;q-icon v-if=&quot;props.rsa &amp;&amp; encryptionType === &#39;rsa&#39;&quot; name=&quot;check_circle&quot; color=&quot;white&quot; class=&quot;q-ml-sm&quot; /&gt;
&lt;/template&gt;

Help!

答案1

得分: 2

You use the slots wrong. The problem is easily recreated.
你的插槽使用方式不正确。问题很容易重现。

You insert this into the default slot of q-btn-toggle:
你将这个插入到q-btn-toggle的默认插槽中:

<div v-if="options.length > 1">

And then inside the default slot you try to insert into name slots which don't exist. Thus node is missing.
然后在默认插槽中,你尝试插入到不存在的名称插槽中。因此出现了node is missing.

To fix you just change your DIVs to <template>. Unfortunately 2 <template>s with v-if and v-for didn't work for me, but it's easily fixed with a ternary expression:
要修复它,只需将你的DIV更改为<template>。不幸的是,带有v-ifv-for的两个<template>对我不起作用,但可以使用三元表达式轻松解决:

The SFC playground is here

<q-btn-toggle v-model="encryptionType" spread class="q-mt-lg" no-caps unelevated :toggle-color="toggleColor" :toggle-text-color="toggleTextColor" :color="color" :text-color="textColor" @update:model-value="checkButtonToggle" :options="options">
    <template v-for="type in options.length > 1 ? options : []" :key="type.value" #[type.value]>
        <q-icon v-if="props[type.value] && encryptionType === type.value" name="check_circle" color="white" class="q-ml-sm" />
    </template>
</q-btn-toggle>

Recreation:
重现:

循环一个数组以用于动态作用域插槽

英文:

You use the slots wrong. The problem is easily recreated.
You insert this into the default slot of q-btn-toggle:

&lt;div v-if=&quot;options.length &gt; 1&quot;&gt;

And then inside the default slot you try to insert into name slots which don't exists. Thus node is missing.

To fix you just change your DIVs to &lt;template&gt;. Unfortunately 2 &lt;template&gt;s with v-if and v-for didn't work for me, but it's easily fixed with a ternar expression:

The SFC playground is here

  &lt;q-btn-toggle v-model=&quot;encryptionType&quot; spread class=&quot;q-mt-lg&quot; no-caps unelevated :toggle-color=&quot;toggleColor&quot; :toggle-text-color=&quot;toggleTextColor&quot; :color=&quot;color&quot; :text-color=&quot;textColor&quot; @update:model-value=&quot;checkButtonToggle&quot; :options=&quot;options&quot;&gt;
      &lt;template v-for=&quot;type in options.length &gt; 1 ? options : []&quot; :key=&quot;type.value&quot; #[type.value]&gt;
          &lt;q-icon v-if=&quot;props[type.value] &amp;&amp; encryptionType === type.value&quot; name=&quot;check_circle&quot; color=&quot;white&quot; class=&quot;q-ml-sm&quot; /&gt;
      &lt;/template&gt;
  &lt;/q-btn-toggle&gt;

Recreation:

循环一个数组以用于动态作用域插槽

答案2

得分: -1

Thanks to @alexander-nenashev, I got an idea on how to fix it. Simplified, it looks like this:

  &lt;q-btn-toggle v-model=&quot;encryptionType&quot; spread class=&quot;q-mt-lg&quot; no-caps unelevated :toggle-color=&quot;toggleColor&quot;
    :toggle-text-color=&quot;toggleTextColor&quot; :color=&quot;color&quot; :text-color=&quot;textColor&quot; @update:model-value=&quot;checkButtonToggle&quot;
    :options=&quot;options&quot;&gt;
    &lt;template v-for=&quot;type in options&quot; #[type.value]&gt;
      &lt;q-icon v-if=&quot;props[type.value] &amp;&amp; encryptionType === type.value&quot; name=&quot;check_circle&quot; color=&quot;white&quot;
        class=&quot;q-ml-sm&quot; /&gt;
    &lt;/template&gt;
  &lt;/q-btn-toggle&gt;
英文:

Thanks to @alexander-nenashev, I got an idea on how to fix it. Simplified, it looks like this:

  &lt;q-btn-toggle v-model=&quot;encryptionType&quot; spread class=&quot;q-mt-lg&quot; no-caps unelevated :toggle-color=&quot;toggleColor&quot;
    :toggle-text-color=&quot;toggleTextColor&quot; :color=&quot;color&quot; :text-color=&quot;textColor&quot; @update:model-value=&quot;checkButtonToggle&quot;
    :options=&quot;options&quot;&gt;
    &lt;template v-for=&quot;type in options&quot; #[type.value]&gt;
      &lt;q-icon v-if=&quot;props[type.value] &amp;&amp; encryptionType === type.value&quot; name=&quot;check_circle&quot; color=&quot;white&quot;
        class=&quot;q-ml-sm&quot; /&gt;
    &lt;/template&gt;
  &lt;/q-btn-toggle&gt;

huangapple
  • 本文由 发表于 2023年6月26日 17:41:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76555458.html
匿名

发表评论

匿名网友

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

确定