if else if vue
Certainly! Here's an example of using v-if
, v-else-if
, and v-else
in Vue.js along with explanations for each step:
<template>
<div>
<p v-if="status === 'success'">Success!</p>
<p v-else-if="status === 'pending'">Pending...</p>
<p v-else>Error!</p>
</div>
</template>
<script>
export default {
data() {
return {
status: 'success' // Change this value to see different outputs
};
}
};
</script>
Explanation:
1. <p v-if="status === 'success'">Success!</p>
: This line checks if the status
data property is equal to 'success'
. If it is, it will display the "Success!" message.
<p v-else-if="status === 'pending'">Pending...</p>
: If thestatus
is not'success'
, this line checks if thestatus
is'pending'
. If it is, it will display the "Pending..." message.<p v-else>Error!</p>
: If thestatus
is neither'success'
nor'pending'
, this line serves as a fallback and displays the "Error!" message.In the JavaScript section (
<script>
), thedata()
method initializes thestatus
data property with the value'success'
. You can change this value to'pending'
or anything else to see different messages based on the condition checks in the template.