Vue JS Ternary Operator Example
Mar 13, 2021
Hi Guys,
Today,I will learn tou how to use ternary operator in vue js. we will show example of vue js ternary operator. we can easily use ternary operator for condition in vue js.
You can easily apply ternary operator with v-model in vue js. you can see both example simple and using v-model too.
ternary condition is key of if condition. if you have small condition in your project than you never want to write if condition and long line of code beside that. But you will prefer ternary condition with single line and easy to use.
I will give you two example:
-> ternary condition for v-model
->ternary condition for simple
Ternary Condition For v-model
<!DOCTYPE html>
<html>
<head>
<title>How to use ternary operator in Vuejs? - nicesnippets.com</title>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
</head>
<body>
<div id="app">
<input type="text" v-model="$data[myCondition ? 'name' : 'title']">
<div>Name: {{ name }}</div>
<div>Title: {{ title }}</div>
</div>
<script type="text/javascript">
var app = new Vue({
el: '#app',
data: {
name: '',
title: '',
myCondition:true
}
})
</script>
</body>
</html>
Ternary Condition For Simple
<!DOCTYPE html>
<html>
<head>
<title>How to use ternary operator in Vuejs? - nicesnippets.com</title>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
</head>
<body>
<div id="app">
<div>Name: {{ myVar == 1 ? 'Hardik' : 'Dharmik' }}</div>
</div>
<script type="text/javascript">
var app = new Vue({
el: '#app',
data: {
myVar:1
}
})
</script>
</body>
</html>
It will help you..
- How to Build VUE JS CRUD Using Node js Express and MySQL?
- Vue js File Upload Axios Php Example
- Vue Js Application Check Current Version In Ubuntu
- Vue JS Get Array Length Or Object Length Tutorial
- Vue JS Download File using Axios Example
- Vue JS Declare Global Variable Example
- Vue Js Sweetalert Modal Notification Tutorial
- Vue Js Get Element By Id Example