如何使用Vue和数组创建条件。

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

How to use Vue and Array to make a conditions

问题

<span v-if="'{{ variant_selection.option1 }} / {{ value }}' !== '{{ variant_selection.option1 }} / XS' ">.</span>
英文:

I have values XS S M loaded as HTML for all sizes Black White and I have a JSON show available sizes of each color.

I want to add v-if when the XS is not available in the array variant_selection.size by using variant_selection.option1 and the values that loaded as HTML XS S M.

Example:

 variant_selection: {
          option1: "Black",
          option2: "XS",
          size: [
          "Black / XS", /// Only black color has XS size
          "Black / S",
          "Black / M",
          "White / S",
          "White / M",
          ],
        },

I was trying that but didn't work for me, please help

<span v-if="'variant_selection.option1 / {{ value }}' !== 'variant_selection.size.includes(variant_selection.option1) / {{ value }}' ">.</span>

答案1

得分: 2

    <span v-if="selection.size.includes(`${color} / ${size}` )">
英文:
<span v-if="variant_selection.size.includes(variant_selection.option1 + ' / ' + value)" ..

or

<span v-if="variant_selection.size.includes(`${variant_selection.option1} / ${value}`)" ..

You should not use Mustaches {{}} in the Vue v-if conditions

See, Attribute Bindings

> Mustaches cannot be used inside HTML attributes. Instead, use a
> v-bind directive

Solution

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const { createApp,ref } = Vue

const App = {
  setup() {
    const color = ref(&#39;&#39;)
    const colors = [&#39;Black&#39;,&#39;White&#39;]
    const size = ref(&#39;&#39;)
    const sizes = [&#39;XS&#39;,&#39;S&#39;, &#39;M&#39;]
    const selection =  {
          size: [
          &quot;Black / XS&quot;, /// Only black color has XS size
          &quot;Black / S&quot;,
          &quot;Black / M&quot;,
          &quot;White / S&quot;,
          &quot;White / M&quot;,
          ]
    }
    return {
      color, colors, size, sizes, selection
    }
  }
}

const app = createApp(App)
app.mount(&#39;#app&#39;)

<!-- language: lang-css -->

#app { line-height: 1.75; }
[v-cloak] { display: none; }

<!-- language: lang-html -->

&lt;div id=&quot;app&quot; v-cloak&gt;
  Color: &lt;select v-model=&quot;color&quot;&gt;
  &lt;option v-for=&quot;c in colors&quot;&gt;{{c}}&lt;/option&gt;
  &lt;/select&gt;&lt;br/&gt;
  Size: &lt;select v-model=&quot;size&quot;&gt;
  &lt;option v-for=&quot;s in sizes&quot;&gt;{{s}}&lt;/option&gt;
  &lt;/select&gt;&lt;br/&gt;
  Combination: {{ `${color} / ${size}` }}&lt;br/&gt;
  &lt;span v-if=&quot;selection.size.includes(`${color} / ${size}` )&quot;&gt;Combination is present&lt;/span&gt;
&lt;/div&gt;
&lt;script src=&quot;https://unpkg.com/vue@3/dist/vue.global.prod.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

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

发表评论

匿名网友

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

确定