2017-09-14 23 views
0

我試圖用隱藏一個div元素的V-if指令中的HTML元素像這樣:VueJS2:V-如果有多個表達式不起作用

<template v-if="(selectedProducts != 'Static Graphic' || selectedProducts != 'Other Support')"> 
    <div><strong>Content 2 Here</strong></div> 
</template> 

但是,這是行不通的。

寫這個邏輯/表達式的正確方法是什麼? MY JSFIDDLE demo

謝謝

+0

如此看來,根據VueJS文檔,多VueJS2中的指令不允許使用表達式。我讀過對嗎?我應該將這個邏輯移入計算屬性中? – redshift

回答

0

VueJS有真棒文件,所以你應該看看這裏第一https://vuejs.org/v2/guide/conditional.html

在這裏,你可以跳過模板標籤和v-如果進入這樣的DIV:

<div v-if="selectedProducts != 'Static Graphic' || selectedProducts != 'Other Support'"> 
Now you see me 
</div> 

到這裏看看:https://jsfiddle.net/bqe05f2w/1/

更新您的特定情況:

<template v-if="selectedProducts.indexOf('Static Graphic') > 0 || selectedProducts.indexOf('Other Support') > 0"> 
    <div><strong>Content 2 Here</strong></div> 
</template> 
1

您可以在v-if中放置一個表達式。 問題是與你的表達:selectedProducts是一個數組,你不能測試selectedProducts != 'Static Graphic'

試試這個:

v-if="(selectedProducts.indexOf('Static Graphic') < 0 && selectedProducts.indexOf('Other Support') < 0)"> 

的jsfiddle:https://jsfiddle.net/thierry36t/x2kc55p0/1/