Quick Recall Vue Js
<script>
new Vue({ // Create the object of view
el: '#app', // targeted element
data: { // Reactive Data
message: 'Hello Vue !'
},
methods: { // Methods or Functions
}
});
</script>
Interpolation
Directives
-- Use to bind properties.
Data Binding :
<a v-bind:href="links.link" :class="links.class" :alt="links.alt">{{links.text}}</a>
Event Handling
:class="{className:Condition}"
Flow
index.html (#app) --> Main.Js (createApp) --> App.Vue (component[HelloWorld.Vue]) --> HelloWorld.Vue (props)
Component
Two Way Binding
Conditional Rendering
Loop
Pass Data To Child Component --> PROPS
Child Component
<template>
<h1> Child Component </h1>
<h3>{{name}}</h3>
<h5>{{data.gender}}</h5>
<button v-on:click="fn_pass">Click</button>
</template>
<script>
export default {
name: 'ChildComp',
props: {
name: String,
data: Object,
fn_pass: Function
}
}
</script>
Parent Component
<template>
<ChildComp :name="mobile" v-bind:data="student" :fn_pass="Test" />
</template>
<script>
import ChildComp from './Child.vue'
export default {
name: 'HomeComp',
components: {
ChildComp
},
date() {
return {
mobile: 123,
student: {
name: "Akshay",
mobile: 123
}
}
},
methods: {
fn_pass() {
alert("Function Get Called");
}
}
}
</script>
Dynamic Classes
How to transfer data from Child to parent ??
Ref
this.$refs.refName.focus();
this.$refs.refName.style.color='red';
// Read text from tags having ref attribute
this.$refs['refName'].textContent
Modifiers
Non-Props Data
Computed Property
Watchers
<template>
<h1> Watch Properties</h1>
<h3>{{count}}</h3>
<button @click='count++'>+</button>
<button @click='count--'>-</button>
</template>
<script>
export default {
name: 'WatchComp',
data() {
return {
count: 0
}
},
watch: {
count(val) {
if (val >= 5) {
console.log('Limit Reached');
} else if (val < 0) {
this.count = 0;
}
}
}
}
</script>
Slots
Parent Component
<template>
<SlotChildComp>
<template v-slot:header>
<h1>Bottle</h1>
</template>
<template v-slot:main>
<p>This milton brand is absolutly amazing</p>
</template>
<template v-slot:footer>
<button>Buy NOW</button>
</template>
</SlotChildComp>
<SlotChildComp>
<template v-slot:header>
<h1>Bottle</h1>
</template>
<template v-slot:main>
<p>This milton brand is absolutly amazing</p>
</template>
<template v-slot:footer>
<!-- <button>Buy NOW</button> -->
</template>
</SlotChildComp>
</template>
<script>
import SlotChildComp from "./SlotChild.vue";
export default {
name: 'SlotParentComp',
components: {
SlotChildComp
},
data() {
return {
count: 0
}
},
}
</script>
Child Component
<template>
<div class="cart">
<header>
<slot name='header'></slot>
</header>
<div>
<slot name='main'></slot>
</div>
<footer>
<slot name='footer'><button>Click Me</button></slot>
</footer>
</div>
</template>
<script>
export default {
name: 'SlotChildComp',
}
</script>
<style>
.cart {
width: 200px;
border: 1px solid black;
padding: 20px;
text-align: center;
}
</style>
Dynamic Components
<template>
<h1>Dynamic Component</h1>
<button @click='compo="DynamicReactComp"'>REACT</button>
<button @click='compo="DynamicAngularComp"'>ANGULAR</button>
<button @click='compo="DynamicVueComp"'>Vue</button>
<div class='container'>
<component :is='compo' />
</div>
</template>
<script>
import DynamicReactComp from "./DynamicReact.vue";
import DynamicAngularComp from "./DynamicAngular.vue";
import DynamicVueComp from "./DynamicVue.vue";
export default {
name: 'DynamicComp',
components: {
DynamicReactComp,
DynamicAngularComp,
DynamicVueComp
},
data() {
return {
compo: 'DynamicReactComp'
}
},
}
</script>
<style scoped>
.container {
width: 300px;
padding: 20px;
text-align: center;
margin: auto;
border: 1px solid black;
border-radius: 10px;
margin-top: 10px;
}
</style>
Teleport Component
<teleport to='#footer'>
<TeleportComp/>
</teleport>
Lifecycle Method
>> Whenever component get removed from DOM | UI then this will get called.
>> Smooth, less memory or management of memory.
>> Whatever actions want to do before UI component remove can do in beforeUnmount.
8. unmounted : Whatever actions want to do after UI component remove can do in unmounted.
Routing
--> Define Routes
router = [{name:'Home',path:'/',component:HomeComp}]
<router-link to='/'>Home</router-link> --> for links
<router-view></router-view> ---> for component loading
--> Redirect from one component to other component
this.$router.push({name:'Home'});
Dynamic routing
{
name:'Profile',
path:'/profile/:name', // dynamic property
component:ProfileComp
},
import { useRoute } from "vue-router";
useRoute().params.name; // values can be read by this approach.
PageNotFound
{
name:'PageNotFound',
path:'/:pathMatch(.*)*', // this use for page not found
component:PageNotFoundComp
},
Here is demo of small CRUD operations using Vue.Js