1. Create a New Vue Project
vue create my-project
2. Install Vue CLI
npm install -g @vue/cli
3. Create a New Vue Component
// MyComponent.vue
<template>
<div>Hello, World!</div>
</template>
<script>
export default {
name: 'MyComponent'
}
</script>
<style scoped>
/* Add styles here */
</style>
4. Use Vue Router
// main.js
import Vue from 'vue'
import VueRouter from 'vue-router'
import App from './App.vue'
Vue.use(VueRouter)
const routes = [
{
path: '/',
name: 'home',
component: Home
},
{
path: '/about',
name: 'about',
component: About
}
]
const router = new VueRouter({
routes
})
new Vue({
router,
render: h => h(App)
}).$mount('#app')
5. Use Vuex for State Management
// store.js
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const store = new Vuex.Store({
state: {
count: 0
},
mutations: {
increment(state) {
state.count++
}
},
actions: {
increment({ commit }) {
commit('increment')
}
}
})
export default store
6. Use Vue's Built-in Directives
// MyComponent.vue
<template>
<div v-if="show">Hello, World!</div>
<div v-else>Goodbye, World!</div>
</template>
<script>
export default {
data() {
return {
show: true
}
}
}
</script>
7. Use Vue's Built-in Filters
// MyComponent.vue
<template>
<div>{{ message | uppercase }}</div>
</template>
<script>
export default {
data() {
return {
message: 'hello world'
}
},
filters: {
uppercase(value) {
return value.toUpperCase()
}
}
}
</script>
8. Use Vue's Built-in Lifecycle Hooks
// MyComponent.vue
<template>
<div>Hello, World!</div>
</template>
<script>
export default {
mounted() {
console.log('Component mounted')
}
}
</script>
9. Use Vue's Built-in Event Handling
// MyComponent.vue
<template>
<button @click="handleClick">Click me!</button>
</template>
<script>
export default {
methods: {
handleClick() {
console.log('Button clicked')
}
}
}
</script>
10. Use Vue's Built-in Form Handling
// MyComponent.vue
<template>
<form @submit.prevent="handleSubmit">
<input type="text" v-model="name" />
<button type="submit">Submit</button>
</form>
</template>
<script>
export default {
data() {
return {
name: ''
}
},
methods: {
handleSubmit() {
console.log('Form submitted')
}
}
}
</script>
11. Use Vue's Built-in Animation and Transition
// MyComponent.vue
<template>
<transition name="fade">
<div v-if="show">Hello, World!</div>
</transition>
</template>
<script>
export default {
data() {
return {
show: true
}
}
}
</script>
<style>
.fade-enter-active, .fade-leave-active {
transition: opacity .5s;
}
.fade-enter, .fade-leave-to {
opacity: 0;
}
</style>
12. Use Vue's Built-in Internationalization
// MyComponent.vue
<template>
<div>{{ $t('hello') }}</div>
</template>
<script>
export default {
i18n: {
messages: {
en: {
hello: 'Hello, World!'
},
fr: {
hello: 'Bonjour, monde!'
}
}
}
}
}
</script>
13. Use Vue's Custom Directives
// main.js
Vue.directive('focus', {
// When the bound element is inserted into the DOM...
inserted: function (el) {
// Focus the element
el.focus()
}
})
// MyComponent.vue
<template>
<input v-focus />
</template>
14. Use Vue's Slots
// MyComponent.vue
<template>
<div>
<slot></slot>
</div>
</template>
// ParentComponent.vue
<template>
<MyComponent>
<p>This is a slot content!</p>
</MyComponent>
</template>
15. Use Vue's Scoped Slots
// MyComponent.vue
<template>
<div>
<slot :message="message"></slot>
</div>
</template>
<script>
export default {
data() {
return {
message: 'Hello from scoped slot!'
}
}
}
</script>
// ParentComponent.vue
<template>
<MyComponent>
<template v-slot:default="slotProps">
<p>{{ slotProps.message }}</p>
</template>
</MyComponent>
</template>
16. Use Vue's Mixins
// mixin.js
export const myMixin = {
data() {
return {
mixinData: 'Hello from mixin!'
}
}
}
// MyComponent.vue
<template>
<div>{{ mixinData }}</div>
</template>
<script>
import { myMixin } from './mixin'
export default {
mixins: [myMixin]
}
</script>
17. Use Vue's Provide/Inject
// ParentComponent.vue
<template>
<ChildComponent />
</template>
<script>
export default {
provide() {
return {
message: 'Hello from parent!'
}
}
}
// ChildComponent.vue
<template>
<div>{{ message }}</div>
</template>
<script>
export default {
inject: ['message']
}
</script>
18. Use Vue's Async Components
// main.js
const AsyncComponent = () => import('./AsyncComponent.vue')
// App.vue
<template>
<AsyncComponent />
</template>
19. Use Vue's Filters
// MyComponent.vue
<template>
<div>{{ message | capitalize }}</div>
</template>
<script>
export default {
data() {
return {
message: 'hello world'
}
},
filters: {
capitalize(value) {
return value.charAt(0).toUpperCase() + value.slice(1)
}
}
}
</script>
20. Use Vue's Event Bus
// eventBus.js
import Vue from 'vue'
export const EventBus = new Vue()
// ComponentA.vue
import { EventBus } from './eventBus'
EventBus.$emit('eventName', data)
// ComponentB.vue
import { EventBus } from './eventBus'
EventBus.$on('eventName', (data) => {
console.log(data)
})
21. Use Vue's Watchers
// MyComponent.vue
<template>
<div>{{ count }}</div>
</template>
<script>
export default {
data() {
return {
count: 0
}
},
watch: {
count(newValue) {
console.log('Count changed to:', newValue)
}
}
}
</script>
22. Use Vue's Computed Properties
// MyComponent.vue
<template>
<div>{{ doubledCount }}</div>
</template>
<script>
export default {
data() {
return {
count: 1
}
},
computed: {
doubledCount() {
return this.count * 2
}
}
}
</script>
23. Use Vue's Dynamic Components
// App.vue
<template>
<component :is="currentComponent"></component>
</template>
<script>
export default {
data() {
return {
currentComponent: 'MyComponent'
}
}
}
</script>
24. Use Vue's Transition Group
// MyComponent.vue
<template>
<transition-group name="fade" tag=" div">
<div v-for="item in items" :key="item.id">{{ item.text }}</div>
</transition-group>
</template>
<script>
export default {
data() {
return {
items: [
{ id: 1, text: 'Item 1' },
{ id: 2, text: 'Item 2' },
{ id: 3, text: 'Item 3' }
]
}
}
}
</script>
<style>
.fade-enter-active, .fade-leave-active {
transition: opacity .5s;
}
.fade-enter, .fade-leave-to {
opacity: 0;
}
</style>
25. Use Vue's Global Components
// main.js
import Vue from 'vue'
import MyComponent from './MyComponent.vue'
Vue.component('MyComponent', MyComponent)
// App.vue
<template>
<MyComponent />
</template>
26. Use Vue's Custom Events
// MyComponent.vue
<template>
<button @click="$emit('custom-event', 'Hello!')">Click me!</button>
</template>
// ParentComponent.vue
<template>
<MyComponent @custom-event="handleCustomEvent" />
</template>
<script>
export default {
methods: {
handleCustomEvent(message) {
console.log(message)
}
}
}
</script>
27. Use Vue's v-model for Two-Way Data Binding
// MyComponent.vue
<template>
<input v-model="name" />
<p>Your name is: {{ name }}</p>
</template>
<script>
export default {
data() {
return {
name: ''
}
}
}
</script>
28. Use Vue's v-for for Rendering Lists
// MyComponent.vue
<template>
<ul>
<li v-for="item in items" :key="item.id">{{ item.text }}</li>
</ul>
</template>
<script>
export default {
data() {
return {
items: [
{ id: 1, text: 'Item 1' },
{ id: 2, text: 'Item 2' },
{ id: 3, text: 'Item 3' }
]
}
}
}
</script>
29. Use Vue's v-bind for Dynamic Attributes
// MyComponent.vue
<template>
<img v-bind:src="imageSrc" alt="Image" />
</template>
<script>
export default {
data() {
return {
imageSrc: 'https://example.com/image.jpg'
}
}
}
</script>
30. Use Vue's v-if and v-else for Conditional Rendering
// MyComponent.vue
<template>
<div>
<p v-if="isVisible">Visible</p>
<p v-else>Not Visible</p>
</div>
</template>
<script>
export default {
data() {
return {
isVisible: true
}
}
}
</script>
31. Use Vue's v-show for Toggling Visibility
// MyComponent.vue
<template>
<div>
<p v-show="isVisible">Visible</p>
<button @click="isVisible = !isVisible">Toggle</button>
</div>
</template>
<script>
export default {
data() {
return {
isVisible: true
}
}
}
</script>
32. Use Vue's v-cloak for Preventing Flash of Unstyled Content
// MyComponent.vue
<template>
<div v-cloak>
<p>Hello, World!</p>
</div>
</template>
<style>
[v-cloak] {
display: none;
}
</style>
33. Use Vue's v-pre for Skipping Compilation
// MyComponent.vue
<template>
<div v-pre>
<p>{{ this will not be compiled }}</p>
</div>
</template>
34. Use Vue's v-on for Event Handling
// MyComponent.vue
<template>
<button v-on:click="handleClick">Click me!</button>
</template>
<script>
export default {
methods: {
handleClick() {
console.log('Button clicked')
}
}
</script>
35. Use Vue's v-slot for Named Slots
// MyComponent.vue
<template>
<div>
<slot name="header"></slot>
<slot></slot>
<slot name="footer"></slot>
</div>
</template>
// ParentComponent.vue
<template>
<MyComponent>
<template v-slot:header>
<h1>Header Content</h1>
</template>
<p>Main Content</p>
<template v-slot:footer>
<p>Footer Content</p>
</template>
</MyComponent>
</template>
36. Use Vue's v-bind for Dynamic Classes
// MyComponent.vue
<template>
<div v-bind:class="{ active: isActive }">Hello, World!</div>
</template>
<script>
export default {
data() {
return {
isActive: true
}
}
}
</script>
37. Use Vue's v-bind for Dynamic Styles
// MyComponent.vue
<template>
<div v-bind:style="{ color: textColor }">Hello, World!</div>
</template>
<script>
export default {
data() {
return {
textColor: 'red'
}
}
}
</script>
38. Use Vue's v-on for Event Modifiers
// MyComponent.vue
<template>
<button v-on:click.prevent="handleClick">Click me!</button>
</template>
<script>
export default {
methods: {
handleClick() {
console.log('Button clicked')
}
}
}
</script>
39. Use Vue's v-model for Custom Components
// MyInput.vue
<template>
<input :value="value" @input="$emit('input', $event.target.value)" />
</template>
<script>
export default {
props: ['value']
}
</script>
// ParentComponent.vue
<template>
<MyInput v-model="name" />
</template>
<script>
export default {
data() {
return {
name: ''
}
}
}
</script>
40. Use Vue's Filters for Formatting
// MyComponent.vue
<template>
<div>{{ price | currency }}</div>
</template>
<script>
export default {
data() {
return {
price: 100
}
},
filters: {
currency(value) {
return `$${value.toFixed(2)}`
}
}
}
</script>
41. Use Vue's Global Mixins
// main.js
Vue.mixin({
created() {
console.log('Global mixin created hook')
}
})
// MyComponent.vue
<template>
<div>Hello, World!</div>
</template>
42. Use Vue's Custom Plugins
// myPlugin.js
export default {
install(Vue) {
Vue.prototype.$myMethod = function() {
console.log('My method called!')
}
}
}
// main.js
import MyPlugin from './myPlugin'
Vue.use(MyPlugin)
// MyComponent.vue
<template>
<button @click="$myMethod()">Call My Method</button>
</template>
43. Use Vue's Async Data Fetching
// MyComponent.vue
<template>
<div>{{ data }}</div>
</template>
<script>
export default {
data() {
return {
data: null
}
},
async created() {
const response = await fetch('https://api.example.com/data')
this.data = await response.json()
}
}
</script>
44. Use Vue's Dynamic Imports
// MyComponent.vue
<template>
<button @click="loadComponent">Load Component</button>
<component :is="asyncComponent"></component>
</template>
<script>
export default {
data() {
return {
asyncComponent: null
}
},
methods: {
async loadComponent() {
this.asyncComponent = (await import('./AsyncComponent.vue')).default
}
}
}
</script>
45. Use Vue's Watchers for Deep Watching
// MyComponent.vue
<template>
<div>{{ items }}</div>
</template>
<script>
export default {
data() {
return {
items: []
}
},
watch: {
items: {
handler(newValue) {
console.log('Items changed:', newValue)
},
deep: true
}
}
}
</script>
46. Use Vue's Computed Properties for Caching
// MyComponent.vue
<template>
<div>{{ fullName }}</div>
</template>
<script>
export default {
data() {
return {
firstName: 'John',
lastName: 'Doe'
}
},
computed: {
fullName() {
return `${this.firstName} ${this.lastName}`
}
}
}
</script>
47. Use Vue's Event Handling with Modifiers
// MyComponent.vue
<template>
<button @click.stop="handleClick">Click me!</button>
</template>
<script>
export default {
methods: {
handleClick() {
console.log('Button clicked')
}
}
}
</script>
48. Use Vue's v-bind for Object Syntax
// MyComponent.vue
<template>
<div v-bind="divAttributes">Hello, World!</div>
</template>
<script>
export default {
data() {
return {
divAttributes: {
id: 'myDiv',
class: 'my-class'
}
}
}
}
</script>
49. Use Vue's v-on for Multiple Event Handlers
// MyComponent.vue
<template>
<button v-on:click="handleClick1" v-on:click="handleClick2">Click me!</button>
</template>
<script>
export default {
methods: {
handleClick1() {
console.log('First handler')
},
handleClick2() {
console.log('Second handler')
}
}
}
</script>
50. Use Vue's v-if with v-else-if and v-else
// MyComponent.vue
<template>
<div>
<p v-if="status === 'loading'">Loading...</p>
<p v-else-if="status === 'error'">Error occurred!</p>
<p v-else>Data loaded successfully!</p>
</div>
</template>
<script>
export default {
data() {
return {
status: 'loading'
}
}
}
</script>
51. Use Vue's v-for with Index
// MyComponent.vue
<template>
<ul>
<li v-for="(item, index) in items" :key="index">{{ index + 1 }}. {{ item }}</li>
</ul>
</template>
<script>
export default {
data() {
return {
items: ['Item 1', 'Item 2', 'Item 3']
}
}
}
</script>
52. Use Vue's v-bind for Dynamic Classes with Conditions
// MyComponent.vue
<template>
<div :class="{ active: isActive, 'text-danger': hasError }">Hello, World!</div>
</template>
<script>
export default {
data() {
return {
isActive: true,
hasError: false
}
}
}
</script>
53. Use Vue's v-bind for Dynamic Styles with Computed Properties
// MyComponent.vue
<template>
<div :style="styleObject">Hello, World!</div>
</template>
<script>
export default {
data() {
return {
isRed: true
}
},
computed: {
styleObject() {
return {
color: this.isRed ? 'red' : 'blue'
}
}
}
}
</script>
54. Use Vue's v-on for Key Modifiers
// MyComponent.vue
<template>
<input @keyup.enter="handleEnter" />
</template>
<script>
export default {
methods: {
handleEnter() {
console.log('Enter key pressed')
}
}
}
</script>
55. Use Vue's v-model with Custom Components
// MyInput.vue
<template>
<input :value="value" @input="$emit('input', $event.target.value)" />
</template>
<script>
export default {
props: ['value']
}
</script>
// ParentComponent.vue
<template>
<MyInput v-model="name" />
</template>
<script>
export default {
data() {
return {
name: ''
}
}
}
</script>
56. Use Vue's v-for with Object Syntax
// MyComponent.vue
<template>
<ul>
<li v-for="(value, key) in items" :key="key">{{ key }}: {{ value }}</li>
</ul>
</template>
<script>
export default {
data() {
return {
items: {
item1: 'Value 1',
item2: 'Value 2',
item3: 'Value 3'
}
}
}
}
</script>
57. Use Vue's v-if with v-else-if and v-else for Complex Conditions
// MyComponent.vue
<template>
<div>
<p v-if="status === 'loading'">Loading...</p>
<p v-else-if="status === 'error'">Error occurred!</p>
<p v-else>Data loaded successfully!</p>
</div>
</template>
<script>
export default {
data() {
return {
status: 'loading'
}
}
}
</script>
58. Use Vue's v-show for Conditional Rendering
// MyComponent.vue
<template>
<div>
<p v-show="isVisible">Visible</p>
<button @click="isVisible = !isVisible">Toggle</button>
</div>
</template>
<script>
export default {
data() {
return {
isVisible: true
}
}
}
</script>
59. Use Vue's v-cloak for Preventing Flash of Unstyled Content
// MyComponent.vue
<template>
<div v-cloak>
<p>Hello, World!</p>
</div>
</template>
<style>
[v-cloak] {
display: none;
}
</style>
60. Use Vue's v-pre for Skipping Compilation
// MyComponent.vue
<template>
<div v-pre>
<p>{{ this will not be compiled }}</p>
</div>
</template>
61. Use Vue's v-on for Event Handling
// MyComponent.vue
<template>
<button v-on:click="handleClick">Click me!</button>
</template>
<script>
export default {
methods: {
handleClick() {
console.log('Button clicked')
}
}
}
</script>
62. Use Vue's v-slot for Named Slots
// MyComponent.vue
<template>
<div>
<slot name="header"></slot>
<slot></slot>
<slot name="footer"></slot>
</div>
</template>
// ParentComponent.vue
<template>
<MyComponent>
<template v-slot:header>
<h1>Header Content</h1>
</template>
<p>Main Content</p>
<template v-slot:footer>
<p>Footer Content</p>
</template>
</MyComponent>
</template>
63. Use Vue's v-bind for Dynamic Classes
// MyComponent.vue
<template>
<div v-bind:class="{ active: isActive }">Hello, World!</div>
</template>
<script>
export default {
data() {
return {
isActive: true
}
}
}
</script>
64. Use Vue's v-bind for Dynamic Styles
// MyComponent.vue
<template>
<div v-bind:style="{ color: textColor }">Hello, World!</div>
</template>
<script>
export default {
data() {
return {
textColor: 'red'
}
}
}
</script>
65. Use Vue's v-on for Event Modifiers
// MyComponent.vue
<template>
<button v-on:click.prevent="handleClick">Click me!</button>
</template>
<script>
export default {
methods: {
handleClick() {
console.log('Button clicked')
}
}
}
</script>
66. Use Vue's v-model for Custom Components
// MyInput.vue
<template>
<input : value="value" @input="$emit('input', $event.target.value)" />
</template>
<script>
export default {
props: ['value']
}
</script>
// ParentComponent.vue
<template>
<MyInput v-model="name" />
</template>
<script>
export default {
data() {
return {
name: ''
}
}
}
</script>
67. Use Vue's Filters for Formatting
// MyComponent.vue
<template>
<div>{{ price | currency }}</div>
</template>
<script>
export default {
data() {
return {
price: 100
}
},
filters: {
currency(value) {
return `$${value.toFixed(2)}`
}
}
}
</script>
68. Use Vue's Global Mixins
// main.js
Vue.mixin({
created() {
console.log('Global mixin created hook')
}
})
// MyComponent.vue
<template>
<div>Hello, World!</div>
</template>
69. Use Vue's Custom Plugins
// myPlugin.js
export default {
install(Vue) {
Vue.prototype.$myMethod = function() {
console.log('My method called!')
}
}
}
// main.js
import MyPlugin from './myPlugin'
Vue.use(MyPlugin)
// MyComponent.vue
<template>
<button @click="$myMethod()">Call My Method</button>
</template>
70. Use Vue's Async Data Fetching
// MyComponent.vue
<template>
<div>{{ data }}</div>
</template>
<script>
export default {
data() {
return {
data: null
}
},
async created() {
const response = await fetch('https://api.example.com/data')
this.data = await response.json()
}
}
</script>
71. Use Vue's Dynamic Imports
// MyComponent.vue
<template>
<button @click="loadComponent">Load Component</button>
<component :is="asyncComponent"></component>
</template>
<script>
export default {
data() {
return {
asyncComponent: null
}
},
methods: {
async loadComponent() {
this.asyncComponent = (await import('./AsyncComponent.vue')).default
}
}
}
</script>
72. Use Vue's Watchers for Deep Watching
// MyComponent.vue
<template>
<div>{{ items }}</div>
</template>
<script>
export default {
data() {
return {
items: []
}
},
watch: {
items: {
handler(newValue) {
console.log('Items changed:', newValue)
},
deep: true
}
}
}
</script>
73. Use Vue's Computed Properties for Caching
// MyComponent.vue
<template>
<div>{{ fullName }}</div>
</template>
<script>
export default {
data() {
return {
firstName: 'John',
lastName: 'Doe'
}
},
computed: {
fullName() {
return `${this.firstName} ${this.lastName}`
}
}
}
</script>
74. Use Vue's Event Handling with Modifiers
// MyComponent.vue
<template>
<button @click.stop="handleClick">Click me!</button>
</template>
<script>
export default {
methods: {
handleClick() {
console.log('Button clicked')
}
}
}
</script>
75. Use Vue's v-bind for Object Syntax
// MyComponent.vue
<template>
<div v-bind="divAttributes">Hello, World!</div>
</template>
<script>
export default {
data() {
return {
divAttributes: {
id: 'myDiv',
class: 'my-class'
}
}
}
}
</script>
76. Use Vue's v-on for Multiple Event Handlers
// MyComponent.vue
<template>
<button v-on:click="handleClick1" v-on:click="handleClick2">Click me!</button>
</template>
<script>
export default {
methods: {
handleClick1() {
console.log('First handler')
},
handleClick2() {
console.log('Second handler')
}
}
}
</script>
77. Use Vue's v-if with v-else-if and v-else for Complex Conditions
// MyComponent.vue
<template>
<div>
<p v-if="status === 'loading'">Loading...</p>
<p v-else-if="status === 'error'">Error occurred!</p>
<p v-else>Data loaded successfully!</p>
</div>
</template>
<script>
export default {
data() {
return {
status: 'loading'
}
}
}
</script>
78. Use Vue's v-for with Index
// MyComponent.vue
<template>
<ul>
<li v-for="(item, index) in items" :key="index">{{ index + 1 }}. {{ item }}</li>
</ul>
</template>
<script>
export default {
data() {
return {
items: ['Item 1', 'Item 2', 'Item 3']
}
}
}
</script>
79. Use Vue's v-bind for Dynamic Classes with Conditions
// MyComponent.vue
<template>
<div :class="{ active: isActive, 'text-danger': hasError }">Hello, World!</div>
</template>
<script>
export default {
data() {
return {
isActive: true,
hasError: false
}
}
}
</script>
80. Use Vue's v-bind for Dynamic Styles with Computed Properties
// MyComponent.vue
<template>
<div :style="styleObject">Hello, World!</div>
</template>
<script>
export default {
data() {
return {
isRed: true
}
},
computed: {
styleObject() {
return {
color: this.isRed ? 'red' : 'blue'
}
}
}
}
</script>
81. Use Vue's v-on for Key Modifiers
// MyComponent.vue
<template>
<input @keyup.enter="handleEnter" />
</template>
<script>
export default {
methods: {
handleEnter() {
console.log('Enter key pressed')
}
}
}
</script>
82. Use Vue's v-model with Custom Components
// MyInput.vue
<template>
<input :value="value" @input="$emit('input', $event.target.value)" />
</template>
<script>
export default {
props: ['value']
}
</script>
// ParentComponent.vue
<template>
<MyInput v-model="name" />
</template>
<script>
export default {
data() {
return {
name: ''
}
}
}
</script>
83. Use Vue's Filters for Formatting
// MyComponent.vue
<template>
<div>{{ price | currency }}</div>
</template>
<script>
export default {
data() {
return {
price: 100
}
},
filters: {
currency(value) {
return `$${value.toFixed(2)}`
}
}
}
</script>
84. Use Vue's Global Mixins
// main.js
Vue.mixin({
created() {
console.log('Global mixin created hook')
}
})
// MyComponent.vue
<template>
<div>Hello, World!</div>
</template>
85. Use Vue's Custom Plugins
// myPlugin.js
export default {
install(Vue) {
Vue.prototype.$myMethod = function() {
console.log('My method called!')
}
}
}
// main.js
import MyPlugin from './myPlugin'
Vue.use(MyPlugin)
// MyComponent.vue
<template>
<button @click="$myMethod()">Call My Method</button>
</template>
86. Use Vue's Async Data Fetching
// MyComponent.vue
<template>
<div>{{ data }}</div>
</template>
<script>
export default {
data() {
return {
data: null
}
},
async created() {
const response = await fetch('https://api.example.com/data')
this.data = await response.json()
}
}
</script>
87. Use Vue's Dynamic Imports
// MyComponent.vue
<template <button @click="loadComponent">Load Component</button>
<component :is="asyncComponent"></component>
</template>
<script>
export default {
data() {
return {
asyncComponent: null
}
},
methods: {
async loadComponent() {
this.asyncComponent = (await import('./AsyncComponent.vue')).default
}
}
}
</script>
88. Use Vue's Watchers for Deep Watching
// MyComponent.vue
<template>
<div>{{ items }}</div>
</template>
<script>
export default {
data() {
return {
items: []
}
},
watch: {
items: {
handler(newValue) {
console.log('Items changed:', newValue)
},
deep: true
}
}
}
</script>
89. Use Vue's Computed Properties for Caching
// MyComponent.vue
<template>
<div>{{ fullName }}</div>
</template>
<script>
export default {
data() {
return {
firstName: 'John',
lastName: 'Doe'
}
},
computed: {
fullName() {
return `${this.firstName} ${this.lastName}`
}
}
}
</script>
90. Use Vue's Event Handling with Modifiers
// MyComponent.vue
<template>
<button @click.stop="handleClick">Click me!</button>
</template>
<script>
export default {
methods: {
handleClick() {
console.log('Button clicked')
}
}
}
</script>
91. Use Vue's v-bind for Object Syntax
// MyComponent.vue
<template>
<div v-bind="divAttributes">Hello, World!</div>
</template>
<script>
export default {
data() {
return {
divAttributes: {
id: 'myDiv',
class: 'my-class'
}
}
}
}
</script>
92. Use Vue's v-on for Multiple Event Handlers
// MyComponent.vue
<template>
<button v-on:click="handleClick1" v-on:click="handleClick2">Click me!</button>
</template>
<script>
export default {
methods: {
handleClick1() {
console.log('First handler')
},
handleClick2() {
console.log('Second handler')
}
}
}
</script>
93. Use Vue's v-if with v-else-if and v-else for Complex Conditions
// MyComponent.vue
<template>
<div>
<p v-if="status === 'loading'">Loading...</p>
<p v-else-if="status === 'error'">Error occurred!</p>
<p v-else>Data loaded successfully!</p>
</div>
</template>
<script>
export default {
data() {
return {
status: 'loading'
}
}
}
</script>
94. Use Vue's v-for with Index
// MyComponent.vue
<template>
<ul>
<li v-for="(item, index) in items" :key="index">{{ index + 1 }}. {{ item }}</li>
</ul>
</template>
<script>
export default {
data() {
return {
items: ['Item 1', 'Item 2', 'Item 3']
}
}
}
</script>
95. Use Vue's v-bind for Dynamic Classes with Conditions
// MyComponent.vue
<template>
<div :class="{ active: isActive, 'text-danger': hasError }">Hello, World!</div>
</template>
<script>
export default {
data() {
return {
isActive: true,
hasError: false
}
}
}
</script>
96. Use Vue's v-bind for Dynamic Styles with Computed Properties
// MyComponent.vue
<template>
<div :style="styleObject">Hello, World!</div>
</template>
<script>
export default {
data() {
return {
isRed: true
}
},
computed: {
styleObject() {
return {
color: this.isRed ? 'red' : 'blue'
}
}
}
}
</script>
97. Use Vue's v-on for Key Modifiers
// MyComponent.vue
<template>
<input @keyup.enter="handleEnter" />
</template>
<script>
export default {
methods: {
handleEnter() {
console.log('Enter key pressed')
}
}
}
</script>
98. Use Vue's v-model with Custom Components
// MyInput.vue
<template>
<input :value="value" @input="$emit('input', $event.target.value)" />
</template>
<script>
export default {
props: ['value']
}
</script>
// ParentComponent.vue
<template>
<MyInput v-model="name" />
</template>
<script>
export default {
data() {
return {
name: ''
}
}
}
</script>
99. Use Vue's Filters for Formatting
// MyComponent.vue
<template>
<div>{{ price | currency }}</div>
</template>
<script>
export default {
data() {
return {
price: 100
}
},
filters: {
currency(value) {
return `$${value.toFixed(2)}`
}
}
}
</script>
100. Use Vue's Global Mixins
// main.js
Vue.mixin({
created() {
console.log('Global mixin created hook')
}
})
// MyComponent.vue
<template>
<div>Hello, World!</div>
</template>