# vuejs-interview-questions List of 300 VueJS Interview Questions > Click :star:if you like the project. Pull Requests are highly appreciated. Follow me [@SudheerJonna](https://twitter.com/SudheerJonna) for technical updates. ---

ZTM Logo

> I recommend this [Vue.js course](https://zerotomastery.io/courses/learn-vue-js/?utm_source=github&utm_medium=sponsor&utm_campaign=vue-interview-questions) to become top 10% at Vue and [this coding interview bootcamp](https://zerotomastery.io/courses/learn-data-structures-and-algorithms/?utm_source=github&utm_medium=sponsor&utm_campaign=vue-interview-questions) to ace your coding interview and actually get hired. --- ### Table of Contents ------------------------------------------------------------------- | No. | Questions | |-----|----------------------------------------------------------------------------------------------------------------------------------------------------------------| | 1 | [What is VueJS](#what-is-vuejs) | | 2 | [What are the major features of VueJS](#what-are-the-major-features-of-vuejs) | | 3 | [What are the lifecycle methods of VueJS](#what-are-the-lifecycle-methods-of-vuejs) | | 4 | [What are the different API styles available](#what-are-the-different-api-styles-available) | | 5 | [What are the conditional directives](#what-are-the-conditional-directives) | | 6 | [What is the difference between v-show and v-if directives](#what-is-the-difference-between-v-show-and-v-if-directives) | | 7 | [What is the purpose of v-for directive?](#what-is-the-purpose-of-v-for-directive) | | 8 | [What is a vue instance?](#what-is-vue-instance) | | 9 | [How do you achieve a conditional group of elements?](#how-do-you-achieve-conditional-group-of-elements) | | 10 | [How do you reuse elements with a key attribute?](#how-do-you-reuse-elements-with-key-attribute) | | 11 | [Why should you not use if and for directives together on the same element?](#why-should-not-use-if-and-for-directives-together-on-the-same-element) | | 12 | [Why do you need to use a key attribute on for directive?](#why-do-you-need-to-use-key-attribute-on-for-directive) | | 13 | [What are the array detection mutation methods?](#what-are-the-array-detection-mutation-methods) | | 14 | [What are the array detection non mutation methods?](#what-are-the-array-detection-non-mutation-methods) | | 15 | [What are the caveats of array changes detection?](#what-are-the-caveats-of-array-changes-detection) | | 16 | [What are the caveats of object changes detection?](#what-are-the-caveats-of-object-changes-detection) | | 17 | [How do you use for directive with a range?](#how-do-you-use-v-for-directive-with-a-range) | | 18 | [How do you use for directive on template?](#how-do-you-use-v-for-directive-on-template) | | 19 | [How do you use event handlers?](#how-do-you-use-event-handlers) | | 20 | [What are the event modifiers provided by vue?](#what-are-the-event-modifiers-provided-by-vue) | | 21 | [What are key modifiers?](#what-are-key-modifiers) | | 22 | [How do you define custom key modifier aliases?](#how-do-you-define-custom-key-modifier-aliases) | | 23 | [What are the supported System Modifier Keys?](#what-are-the-supported-system-modifier-keys) | | 24 | [What are the supported Mouse Button Modifiers?](#what-are-the-supported-mouse-button-modifiers) | | 25 | [How do you implement two way binding?](#how-do-you-implement-two-way-binding) | | 26 | [What are the supported modifiers on model?](#what-are-the-supported-modifiers-on-model) | | 27 | [What are components and give an example?](#what-are-components-and-give-an-example) | | 28 | [What are props?](#what-are-props) | | 29 | [When component needs a single root element?](#when-component-needs-a-single-root-element) | | 30 | [How do you communicate from child to parent using events?](#how-do-you-communicate-from-child-to-parent-using-events) | | 31 | [How do you implement model on custom input components?](#how-do-you-implement-model-on-custom-input-components) | | 32 | [What are slots?](#what-are-slots) | | 33 | [What is global registration in components?](#what-is-global-registration-in-components) | | 34 | [Why do you need local registration?](#why-do-you-need-local-registration) | | 35 | [What is the difference between local and global registration in module system?](#what-is-the-difference-between-local-and-global-registration-in-module-system) | | 36 | [What are possible prop types?](#what-are-possible-prop-types) | | 37 | [What is the data flow followed by props?](#what-is-the-data-flow-followed-by-props) | | 38 | [What are non prop attributes?](#what-are-non-prop-attributes) | | 39 | [Describe about validations available for props?](#describe-about-validations-available-for-props) | | 40 | [How do you customize model directive for a component?](#how-do-you-customize-model-directive-for-a-component) | | 41 | [What are the possible ways to provide transitions?](#What-are-the-possible-ways-to-provide-transitions) | | 42 | [What is vue router and their features?](#what-is-vue-router-and-their-features) | | 43 | [What are the steps to use vue router and give an example?](#what-are-the-steps-to-use-vue-router-and-give-an-example) | | 44 | [What is dynamic route matching?](#what-is-dynamic-route-matching) | | 45 | [How to make router param changes as reactive?](#how-to-make-router-param-changes-as-reactive) | | 46 | [What is route matching priority?](#what-is-route-matching-priority) | | 47 | [What are nested routes?](#what-are-nested-routes) | | 48 | [What are single file components?](#what-are-single-file-components) | | 49 | [Is Single File Components violating separation of concerns?](#is-single-file-components-violating-separation-of-concerns) | | 50 | [What are the problems solved by Single File Components?](#what-are-the-problems-solved-by-single-file-components) | | 51 | [What are filters?](#what-are-filters) | | 52 | [What are the different ways to create filters?](#what-are-the-different-ways-to-create-filters) | | 53 | [How do you chain filters](#how-do-you-chain-filters) | | 54 | [Is it possible to pass parameters for filters?](#is-it-possible-to-pass-parameters-for-filters) | | 55 | [What are plugins and their various services?](#what-are-plugins-and-their-various-services) | | 56 | [ How to create a plugin?](#how-to-create-a-plugin) | | 57 | [How to use a plugin?](#how-to-use-a-plugin) | | 58 | [What are mixins?](#what-are-mixins) | | 59 | [What are global mixins?](#what-are-global-mixins) | | 60 | [How do you use mixins in CLI?](#how-do-you-use-mixins-in-cli) | | 61 | [What are the merging strategies in mixins?](#what-are-the-merging-strategies-in-mixins) | | 62 | [What are custom options merging strategies?](#what-are-custom-options-merging-strategies) | | 63 | [What are custom directives?](#what-are-custom-directives) | | 64 | [How do you register directives locally?](#how-do-you-register-directives-locally) | | 65 | [What are the hook functions provided by directives?](#what-are-the-hook-functions-provided-by-directives) | | 66 | [What are the directive Hook Arguments?](#what-are-the-directive-hook-arguments) | | 67 | [How do you pass multiple values to a directive?](#how-do-you-pass-multiple-values-to-a-directive) | | 68 | [What is function shorthand in directive hooks?](#what-is-function-shorthand-in-directive-hooks) | | 69 | [What is the benefit of render functions over templates?](#what-is-the-benefit-of-render-functions-over-templates) | | 69 | [What is a render function?](#What-is-a-render-function) | | 70 | [Explain the structure of createElement with arguments](#explain-the-structure-of-createelement-with-arguments) | | 71 | [How can you write duplicate virtual nodes in a component?](#how-can-you-write-duplicate-virtual-nodes-in-a-component) | | 72 | [List down the template equivalents in render functions?](#list-down-the-template-equivalents-in-render-functions) | | 73 | [What are functional components?](#what-are-functional-components) | | 74 | [What are the similarities between VueJS and ReactJS?](#what-are-the-similarities-between-vuejs-and-reactjs) | | 75 | [What is the difference between VueJS and ReactJS?](#what-is-the-difference-between-vuejs-and-reactjs) | | 76 | [What are the advantages of VueJS over ReactJS?](#what-are-the-advantages-of-vuejs-over-reactjs) | | 77 | [What are the advantages of ReactJS over VueJS?](#what-are-the-advantages-of-reactjs-over-vuejs) | | 78 | [What are the differences between VueJS and Angular?](#What-are-the-differences-between-vuejs-and-angular) | | 79 | [What are dynamic components?](#what-are-dynamic-components) | | 80 | [What is the purpose of keep alive tag?](#what-is-the-purpose-of-keep-alive-tag) | | 81 | [What are async components?](#what-are-async-components) | | 82 | [What is the structure of async component factory?](#what-is-the-structure-of-async-component-factory) | | 83 | [What are inline templates?](#what-are-inline-templates) | | 84 | [What are X Templates?](#what-are-x-templates) | | 85 | [What are recursive components?](#what-are-recursive-components) | | 86 | [How do you resolve circular dependencies between components?](#how-do-you-resolve-circular-dependencies-between-components) | | 87 | [How do you make sure vue application is CSP complaint?](#how-do-you-make-sure-vue-application-is-csp-complaint) | | 88 | [What is the difference between full and runtime only builds?](#what-is-the-difference-between-full-and-runtime-only-builds) | | 89 | [List down different builds of vuejs?](#list-down-different-builds-of-vuejs) | | 90 | [How do you configure vuejs in webpack?](#how-do-you-configure-vuejs-in-webpack) | | 91 | [What is the purpose of vuejs compiler?](#what-is-the-purpose-of-vuejs-compiler) | | 92 | [What is Dev Tools and its purpose?](#what-is-dev-tools-and-its-purpose) | | 93 | [What is the browser support of VueJS?](#what-is-the-browser-support-of-vuejs) | | 94 | [How do you use various CDNs?](#how-do-you-use-various-cdns?) | | 95 | [How do you force update?](#how-do-you-force-update) | | 96 | [What is the purpose of vuejs once directive?](#what-is-the-purpose-of-vuejs-once-directive) | | 97 | [How do you access the root instance?](#how-do-you-access-the-root-instance) | | 98 | [List out top 10 organizations using Vuejs?](#list-out-top-10-organizations-using-vuejs) | | 99 | [What is the purpose of renderError?](#what-is-the-purpose-of-rendererror) | | 100 | [How do you access parent instance?](#how-do-you-access-parent-instance) | | 101 | [What is vuex?](#what-is-vuex) | | 102 | [What are the major components of State Management Pattern?](#what-are-the-major-components-of-state-management-pattern) | | 103 | [How do you represent one way data flow in vuex?](#how-do-you-represent-one-way-data-flow-in-vuex) | | 104 | [What is a vuejs loader?](#what-is-a-vuejs-loader) | | 105 | [How do you configure vue loader in webpack?](#how-do-you-configure-vue-loader-in-webpack) | | 106 | [What are asset url transform rules?](#what-are-asset-url-transform-rules) | | 107 | [How do you work with preprocessors using vue loader?](#how-do-you-work-with-preprocessors-using-vue-loader) | | 108 | [What is scoped CSS?](#What-is-scoped-CSS) | | 109 | [Is it possible to mix both local and global styles?](#is-it-possible-to-mix-both-local-and-global-styles) | | 110 | [How do you use deep selectors?](#how-do-you-use-deepselectors) | | 111 | [Is parent styles leaked into child components in scoped css?](#is-parent-styles-leaked-into-child-components-in-scoped-css) | | 112 | [How do you style dynamic generated content using scoped css?](#how-do-you-style-dynamic-generated-content-using-scoped-css) | | 113 | [Is CSS modules supported in Vuejs?](#is-css-modules-supported-in-vuejs) | | 114 | [Can I use runtime builds for all templates?](#can-i-use-runtime-builds-for-all-templates) | | 115 | [How to use CSS modules in vuejs?](#how-to-use-css-modules-in-vuejs) | | 116 | [Can I use CSS modules for preprocessors?](#can-i-use-css-modules-for-preprocessors) | | 117 | [Is it possible to use custom inject name for CSS modules?](#is-it-possible-to-use-custom-inject-name-for-css-modules) | | 118 | [What is hot reloading in vue loader?](#what-is-hot-reloading-in-vue-loader) | | 119 | [What is the default behavior of hot reloading?](#what-is-the-default-behavior-of-hot-reloading) | | 120 | [How do you disable hot reloading explicitly?](#how-do-you-disable-hot-reloading-explicitly) | | 121 | [How do you use hot reloading?](#how-do-you-use-hot-reloading) | | 122 | [What are state preservation rules in hot reloading?](#what-are-state-preservation-rules-in-hot-reloading) | | 123 | [How to create functional components using vue loader?](#how-to-create-functional-components-using-vue-loader) | | 124 | [How do you access global properties of functional components?](#how-do-you-access-global-properties-of-functional-components) | | 125 | [How do you perform testing in vuejs?](#how-do-you-perform-testing-in-vuejs) | | 126 | [How do you apply linting for css?](#how-do-you-apply-linting-for-css) | | 127 | [How do you use eslint plugin?](#how-do-you-use-eslint-plugin) | | 128 | [What is the purpose of eslint loader?](#what-is-the-purpose-of-eslint-loader) | | 129 | [What is CSS extraction?](#what-is-css-extraction) | | 130 | [What are custom blocks?](#what-are-custom-blocks) | | 131 | [What are the features of stylelint?](#what-are-the-features-of-stylelint?) | | 132 | [What are the principles for vuex application structure?](#what-are-the-principles-for-vuex-application-structure) | | 133 | [Does Vuex support hot reloading??](#does-vuex-support-hot-reloading) | | 134 | [What is the purpose of hotUpdate API of vuex store?](#what-is-the-purpose-of-hotupdate-api-of-vuex-store) | | 135 | [How do you test mutations?](#how-do-you-test-mutations) | | 136 | [How do you test your getters?](#how-do-you-test-your-getters) | | 137 | [What is the procedure to run tests in node?](#what-is-the-procedure-to-run-tests-in-node) | | 138 | [What is the procedure to run tests in browser?](#what-is-the-procedure-to-run-tests-in-browser) | | 139 | [What is the purpose of strict mode in vuex?](#what-is-the-purpose-of-strict-mode-in-vuex) | | 140 | [Can I use strict mode in production environment?](#can-i-use-strict-mode-in-production-environment) | | 141 | [What is vuex plugin?](#what-is-vuex-plugin) | | 142 | [How do you mutate state in plugins?](#how-do-you-mutate-state-in-plugins) | | 143 | [What is vuex store?](#what-is-vuex-store) | | 144 | [What are the differences of vuex store and plain global object?](#what-are-the-differences-of-vuex-store-and-plain-global-object) | | 145 | [What is the reason not to update the state directly?](#what-is-the-reason-not-to-update-the-state-directly) | | 146 | [What is Single state tree?](#what-is-single-state-tree) | | 147 | [How do you install vuex?](#how-do-you-install-vuex) | | 148 | [Do I need promise for vuex?](#do-i-need-promise-for-vuex) | | 149 | [How do you display store state in vue components?](#how-do-you-display-store-state-in-vue-components) | | 150 | [How do you inject store into child components?](#how-do-you-inject-store-into-child-components) | | 151 | [What is mapState helper?](#what-is-mapstate-helper) | | 152 | [How do you combine local computed properties with mapState helper?](#how-do-you-combine-local-computed-properties-with-mapstate-helper) | | 153 | [Do you need to replace entire local state with vuex?](#do-you-need-to-replace-entire-local-state-with-vuex) | | 154 | [What are vuex getters?](#what-are-vuex-getters?) | | 155 | [What is a property style access?](#what-is-a-property-style-access) | | 156 | [What is a method style access?](#what-is-a-method-style-access) | | 157 | [What is mapGetter helper?](#what-is-mapgetter-helper) | | 158 | [What are mutations?](#what-are-mutations) | | 159 | [How do you commit with payload?](#how-do-you-commit-with-payload) | | 160 | [What is object style commit?](#what-is-object-style-commit) | | 161 | [What are the caveats with vuex mutations?](#what-are-the-caveats-with-vuex-mutations) | | 162 | [Why mutations should be synchronous?](#why-mutations-should-be-synchronous) | | 163 | [How do you perform mutations in components?](#how-do-you-perform-mutations-in-components) | | 164 | [Is it mandatory to use constants for mutation types?](#is-it-mandatory-to-use-constants-for-mutation-types) | | 165 | [How do you perform asynchronous operations?](#how-do-you-perform-asynchronous-operations) | | 166 | [What are differences between mutations and actions?](#what-are-differences-between-mutations-and-actions) | | 167 | [Give an example usage of actions?](#give-an-example-usage-of-actions?) | | 168 | [How do you dispatch actions?](#how-do-you-dispatch-actions) | | 169 | [Can you dispatch an action using payload or object?](#can-you-dispatch-an-action-using-payload-or-object) | | 170 | [Can I use styled components in vuejs?](#can-i-use-styled-components-in-vuejs) | | 171 | [How do you dispatch actions in components?](#how-do-you-dispatch-actions-in-components) | | 172 | [How do you compose actions?](#how-do-you-compose-actions) | | 173 | [What are modules in vuex?](#what-are-modules-in-vuex) | | 174 | [What is module local state?](#what-is-module-local-state) | | 175 | [What is namespacing in vuex](#what-is-namespacing-in-vuex) | | 176 | [What is the default namespace behavior in vuex?](#what-is-the-default-namespace-behavior-in-vuex) | | 177 | [When do you reuse modules?](#when-do-you-reuse-modules) | | 178 | [What are the principles enforced by vuex?](#what-are-the-principles-enforced-by-vuex) | | 179 | [Can I perform mutations directly in strict mode?](#can-i-perform-mutations-directly-in-strict-mode) | | 180 | [How to use model directive with two way computed property?](#how-to-use-model-directive-with-two-way-computed-property) | | 181 | [What is Vue CLI?](#what-is-vue-cli) | | 182 | [What are the features provided by Vue CLI?](#what-are-the-features-provided-by-vue-cli) | | 183 | [What is instant prototyping?](#what-is-instant-prototyping) | | 184 | [How do you create project using Vue CLI?](#how-do-you-create-project-using-vue-cli) | | 185 | [How do you create project using GUI?](#how-do-you-create-project-using-gui) | | 186 | [What are plugins in vue CLI?](#what-are-plugins-in-vue-cli) | | 187 | [How do you install plugins in an existing Vue CLI project?](#how-do-you-install-plugins-in-an-existing-vue-cli-project) | | 188 | [How to access local plugins in a project?](#how-to-access-local-plugins-in-a-project) | | 189 | [How do you create UI plugins kind of behavior?](#how-do-you-create-ui-plugins-kind-of-behavior) | | 190 | [What are presets?](#what-are-presets) | | 191 | [What is the versioning behavior in preset plugins?](#what-is-the-versioning-behavior-in-preset-plugins) | | 192 | [How do you allow plugin prompts?](#how-do-you-allow-plugin-prompts) | | 193 | [What are remote presets?](#what-are-remote-presets) | | 194 | [Can I use local presets?](#can-i-use-local-presets) | | 195 | [What is the purpose of browserslist option?](#what-is-the-purpose-of-browserslist-option) | | 196 | [How do you find VueJS version using API?](#how-do-you-find-vuejs-version-using-api) | | 197 | [How do you create reactive objects](#how-do-you-create-reactive-objects) | | 198 | [What is the purpose new slot directive?](#what-is-the-purpose-new-slot-directive) | | 199 | [What is the use of compile method?](#what-is-the-use-of-compile-method) | | 200 | [What does nextTick do in VueJS?](#what-does-nexttick-do-in-vuejs) | | 201 | [What is async error handling?](#what-is-async-error-handling) | | 202 | [What are Dynamic Directive Arguments?](#what-are-dynamic-directive-arguments) | | 203 | [What are the drawbacks of dynamic directive arguments?](#what-are-the-drawbacks-of-dynamic-directive-arguments) | | 204 | [What is the special handling for null values in dynamic directive arguments?](#what-is-the-special-handling-for-null-values-in-dynamic-directive-arguments) | | 205 | [Can I use dynamic directive null value for slots?](#can-i-use-dynamic-directive-null-value-for-slots) | | 206 | [What is Vue I18n plugin?](#what-is-vue-i-8n--plugin) | | 207 | [#What are the types of formatting?](#what-are-the-types-of-formatting) | | 208 | [What is custom formatting?](#what-is-custom-formatting) | | 209 | [How do you handle Pluralization?](#how-do-you-handle-pluralization) | | 210 | [How to implement DateTime localization?](#how-to-implement-date-time-localization) | | 211 | [How do you implement Number localization?](#how-do-you-implement-number-localization) | | 212 | [How do you perform locale changing](#how-do-you-perform-locale-changing) | | 213 | [What is Lazy loading translations?](#what-is-lazy-loading-translations) | | 214 | [What is the main difference between method and computed property?](#what-is-the-main-difference-between-method-and-computed-property) | | 215 | [What is vuetify?](#what-is-vuetify) | | 216 | [How do you watch for nested data changes?](#how-do-you-watch-for-nested-data-changes) | | 217 | [How to trigger watchers on initialization?](#how-to-trigger-watchers-on-initialization) | | 218 | [What is the purpose of comments option?](#what-is-the-purpose-of-comments-option) | | 219 | [How to identify whether code is running on client or server?](#how-to-identify-whether-code-is-running-on-client-or-server) | | 220 | [How do you watch route object changes?](#how-do-you-watch-route-object-changes) | | 221 | [How do you sync current route in vuex store?](#how-do-you-sync-current-route-in-vuex-store) | | 222 | [What are navigation guards in vue router?](#what-are-navigation-guards-in-vue-router) | | 223 | [Can I use computed property in another computed property?](#can-i-use-computed-property-in-another-computed-property) | | 224 | [How can I use imported constant in template section?](#How-can-i-use-imported-constant-in-template-section) | | 225 | [Is recommended to use async for computed properties?](#is-recommended-to-use-async-for-computed-properties) | | 226 | [What happens if you use duplicate field names?](#what-happens-if-you-use-duplicate-field-names) | | 227 | [Why the component data must be a function?](#why-the-component-data-must-be-a-function) | | 228 | [What is the reason for recommendation for multi-word component names?](#what-is-the-reason-for-recommendation-for-multi-word-component-names) | | 229 | [How to use composition API in Vue2.0?](#how-to-use-composition-api-in-vue2.0) | | 230 | [What is composition API?](#what-is-composition-api) | | 231 | [What is the best way to re-render a component?](#what-is-the-best-way-to-re-render-a-component) | 1. ### What is VueJS? **Vue.js** is an open-source, progressive Javascript framework for building user interfaces that aim to be incrementally adoptable. The core library of VueJS is focused on the `view layer` only, and is easy to pick up and integrate with other libraries or existing projects. **[⬆ Back to Top](#table-of-contents)** 2. ### What are the major features of VueJS? Below are the some of major features available with VueJS 1. **Virtual DOM:** It uses virtual DOM similar to other existing frameworks such as ReactJS, Ember etc. Virtual DOM is a light-weight in-memory tree representation of the original HTML DOM and updated without affecting the original DOM. 2. **Components:** Used to create reusable custom elements in VueJS applications. 3. **Templates:** VueJS provides HTML based templates that bind the DOM with the Vue instance data 4. **Routing:** Navigation between pages is achieved through vue-router 5. **Light weight:** VueJS is light weight library compared to other frameworks. **[⬆ Back to Top](#table-of-contents)** 3. ### What are the lifecycle methods of VueJS? Lifecycle hooks are a window into how the library you're using works behind-the-scenes. By using these hooks, you will know when your component is created, added to the DOM, updated, or destroyed. Let's look at lifecycle diagram before going to each lifecycle hook in detail, 1. **Creation(Initialization):** Creation Hooks allow you to perform actions before your component has even been added to the DOM. You need to use these hooks if you need to set things up in your component both during client rendering and server rendering. Unlike other hooks, creation hooks are also run during server-side rendering. 1. beforeCreate: This hook runs at the very initialization of your component. hook observes data and initialization events in your component. Here, data is still not reactive and events that occur during the component's lifecycle have not been set up yet. ```javascript new Vue({ data: { count: 10 }, beforeCreate: function () { console.log('Nothing gets called at this moment') // `this` points to the view model instance console.log('count is ' + this.count); } }) // count is undefined ``` 2. created: This hook is invoked when Vue has set up events and data observation. Here, events are active and access to reactive data is enabled though templates have not yet been mounted or rendered. ```javascript new Vue({ data: { count: 10 }, created: function () { // `this` points to the view model instance console.log('count is: ' + this.count) } }) // count is: 10 ``` **Note:** Remember that, You will not have access to the DOM or the target mounting element (this.$el) inside of creation hooks 2. **Mounting(DOM Insertion):** Mounting hooks are often the most-used hooks and they allow you to access your component immediately before and after the first render. 1. beforeMount: The beforeMount allows you to access your component immediately before and after the first render. ```javascript new Vue({ beforeMount: function () { // `this` points to the view model instance console.log(`this.$el is yet to be created`); } }) ``` 2. mounted: This is a most used hook and you will have full access to the reactive component, templates, and rendered DOM (via. this.$el). The most frequently used patterns are fetching data for your component. ```javascript

I'm text inside the component.

new Vue({ el: '#app', mounted: function() { console.log(this.$el.textContent); // I'm text inside the component. } }) ``` 3. **Updating (Diff & Re-render):** Updating hooks are called whenever a reactive property used by your component changes, or something else causes it to re-render 1. beforeUpdate: The beforeUpdate hook runs after data changes on your component and the update cycle begins, right before the DOM is patched and re-rendered. ```javascript

{{counter}}

...// rest of the code new Vue({ el: '#app', data() { return { counter: 0 } }, created: function() { setInterval(() => { this.counter++ }, 1000) }, beforeUpdate: function() { console.log(this.counter) // Logs the counter value every second, before the DOM updates. } }) ``` 2. updated: This hook runs after data changes on your component and the DOM re-renders. ```javascript

{{counter}}

...// new Vue({ el: '#app', data() { return { counter: 0 } }, created: function() { setInterval(() => { this.counter++ }, 1000) }, updated: function() { console.log(+this.$refs['dom'].textContent === this.counter) // Logs true every second } }) ``` 4. **Destruction (Teardown):** Destruction hooks allow you to perform actions when your component is destroyed, such as cleanup or analytics sending. 1. beforeDestroy: `beforeDestroy` is fired right before teardown. If you need to cleanup events or reactive subscriptions, beforeDestroy would probably be the time to do it. Your component will still be fully present and functional. ```javascript new Vue ({ data() { return { message: 'Welcome VueJS developers' } }, beforeDestroy: function() { this.message = null delete this.message } }) ``` 2. destroyed: This hooks is called after your component has been destroyed, its directives have been unbound and its event listeners have been removed. ```javascript new Vue ({ destroyed: function() { console.log(this) // Nothing to show here } }) ``` **[⬆ Back to Top](#table-of-contents)** 4. ### What are the different API styles available? The Vue components can be created in two different API styles 1. **Options API:** The Options API uses component logic using an object of options such as `data`, `props`, `computed`, `methods` and life cycle methods etc. The properties will be accessible inside functions using component instance(i.e, `this`). 2. **Composition API:** The Composition API uses component logic using imported API functions. The Single File Components(SFCs) requires `setup` attribute(`

Welcome to Vue routing app!

Home Services

``` **Step 2:** Import Vue and VueRouter packages and then apply router ```javascript import Vue from 'vue'; import VueRouter from 'vue-router'; Vue.use(VueRouter) ``` **Step 3:** Define or import route components. ```javascript const Home = { template: '
Home
' } const Services = { template: '
Services
' } ``` **Step 4:** Define your route where each one maps to a component ```javascript const routes = [ { path: '/home', component: Home }, { path: '/services', component: Services } ] ``` **Step 5:** Create the router instance and pass the `routes` option ```javascript const router = new VueRouter({ routes // short for `routes: routes` }) ``` **Step 6:** Create and mount the root instance. ```javascript const app = new Vue({ router }).$mount('#app') ``` Now you are able to navigate different pages(Home, Services) with in Vue application. **[⬆ Back to Top](#table-of-contents)** 43. ### What is dynamic route matching? Sometimes it may be required to map routes to the same component based on a pattern. Let's take a user component with the mapped URLs like `/user/john/post/123` and `/user/jack/post/235` using dynamic segments, ```javascript const User = { template: '
User {{ $route.params.name }}, PostId: {{ $route.params.postid }}
' } const router = new VueRouter({ routes: [ // dynamic segments start with a colon { path: '/user/:name/post/:postid', component: User } ] }) ``` **[⬆ Back to Top](#table-of-contents)** 44. ### How to make router param changes as reactive? When you navigate from one URL to other(mapped with a single component) using routes with params then the same component instance will be reused. Even though it is more efficient than destroying the old instance and then creating a new one, the lifecycle hooks of the component will not be called. This problem can be solved using either of the below approaches, 1. **Watch the $route object:** ```javascript const User = { template: '
User {{ $route.params.name }}
', watch: { '$route' (to, from) { // react to route changes... } } } ``` 2. **Use beforeRouteUpdate navigation guard:** This is only available since 2.2 version. ```javascript const User = { template: '
User {{ $route.params.name }}
', beforeRouteUpdate (to, from, next) { // react to route changes and then call next() } } ``` Note that the beforeRouteEnter guard does NOT have access to `this`. Instead you can pass a callback to `next` to access the vm instance. **[⬆ Back to Top](#table-of-contents)** 45. ### What is route matching priority? Sometimes the URL might be matched by multiple routes and the confusion of which route need to be mapped is resolved by route matching priority. The priority is based on order of routes configuration. i.e, The route which declared first has higher priority. ```javascript const router = new VueRouter({ routes: [ // dynamic segments start with a colon { path: '/user/:name', component: User } // This route gets higher priority { path: '/user/:name', component: Admin } { path: '/user/:name', component: Customer } ] }) ``` **[⬆ Back to Top](#table-of-contents)** 46. ### What are nested routes? Generally, the app is composed of nested components which are nested multiple levels deep. The segments of a URL corresponds to a certain structure of these nested components. To render components into the nested outlet, you need to use the `children` option in `VueRouter` constructor config. Let's take a user app composed of profile and posts nested components with respective routes. You can also define a default route configuration when there is no matching nested route. ```javascript const router = new VueRouter({ routes: [ { path: '/user/:id', component: User, children: [ { // UserProfile will be rendered inside User's when /user/:id/profile is matched path: 'profile', component: UserProfile }, { // UserPosts will be rendered inside User's when /user/:id/posts is matched path: 'posts', component: UserPosts }, // UserHome will be rendered inside User's when /user/:id is matched { path: '', component: UserHome }, ] } ] }) ``` **[⬆ Back to Top](#table-of-contents)** 47. ### What are single file components? Single File Components are an easy concept to understand. Earlier you might heard about all three parts(HTML, JavaScript and CSS) of your application kept in different components. But Single File Components encapsulate the structure, styling and behaviour into one file. In the beginning, it seems strange to have all three parts in one file, but it actually makes a lot more sense. Let's take an example of Singile File Components ```vue ``` **[⬆ Back to Top](#table-of-contents)** 48. ### Is Single File Components violating separation of concerns? As for the latest modern UI development, separation of concerns is not equal to separation of file types. So it is preferred to divide codebase layers into loosely-coupled components and compose them instead of dividing the codebase into three huge layers that interweave with one another. This way makes Single File Components more cohesive and maintainable by combining template, logic and styles together inside a component. You can also still maintain javascript and CSS files separately with hot-reloading and pre-compilation features. For example, ```vue ``` **[⬆ Back to Top](#table-of-contents)** 49. ### What are the problems solved by Single File Components? The Single File Components solve the common problems occurred in a javascript driven application with a .vue extension. The list of issues are, 1. Global definitions force unique names for every component 2. String templates lack syntax highlighting and require ugly slashes for multiline HTML 3. No CSS support means that while HTML and JavaScript are modularized into components, CSS is conspicuously left out 4. No build step restricts us to HTML and ES5 JavaScript, rather than preprocessors like Pug (formerly Jade) and Babel. **[⬆ Back to Top](#table-of-contents)** 50. ### What are filters? Filters can be used to apply common text formatting. These Filters should be appended to the end of the JavaScript expression, denoted by the "pipe" symbol. You can use them in two specific cases: 1. mustache interpolations 2. v-bind expressions For example, Let's define a local filter named capitalize in a component's options ```javascript filters: { capitalize: function (value) { if (!value) return '' value = value.toString() return value.charAt(0).toUpperCase() + value.slice(1) } } ``` Now you can use the filter in either mustache interpolation or v-bind expression, ```vue {{ username | capitalize }}
``` **[⬆ Back to Top](#table-of-contents)** 51. ### What are the different ways to create filters? You can define filters in two ways, 1. **Local filters:** You can define local filters in a component's options. In this case, filter is applicable to that specific component. ```javascript filters: { capitalize: function (value) { if (!value) return '' value = value.toString() return value.charAt(0).toUpperCase() + value.slice(1) } } ``` 2. **Global filters:** You can also define a filter globally before creating the Vue instance. In this case, filter is applicable to all the components with in the vue instance, ```javascript Vue.filter('capitalize', function (value) { if (!value) return '' value = value.toString() return value.charAt(0).toUpperCase() + value.slice(1) }) new Vue({ // ... }) ``` **[⬆ Back to Top](#table-of-contents)** 52. ### How do you chain filters? You can chain filters one after the other to perform multiple manipulations on the expression. The generic structure of filter chain would be as below, ```vue {{ message | filterA | filterB | filterB ... }} ``` In the above chain stack, you can observe that message expression applied with three filters, each separated by a pipe(|) symbol. The first filter(filterA) takes the expression as a single argument and the result of the expression becomes an argument for second filter(filterB) and the chain continue for remaining filters. For example, if you want to transform date expression with a full date format and uppercase then you can apply dateFormat and uppercase filters as below, ```vue {{ birthday | dateFormat | uppercase }} ``` **[⬆ Back to Top](#table-of-contents)** 53. ### Is it possible to pass parameters for filters? Yes, you can pass arguments for a filter similar to a javascript function. The generic structure of filter parameters would be as follows, ```vue {{ message | filterA('arg1', arg2) }} ``` In this case, filterA takes message expression as first argument and the explicit parameters mentioned in the filter as second and third arguments. For example, you can find the exponential strength of a particular value ```vue {{ 2 | exponentialStrength(10) }} ``` **[⬆ Back to Top](#table-of-contents)** 54. ### What are plugins and their various services? Plugins provides global-level functionality to Vue application. The plugins provide various services, 1. Add some global methods or properties. For example, vue-custom-element 2. Add one or more global assets (directives, filters and transitions). For example, vue-touch 3. Add some component options by global mixin. For example, vue-router 4. Add some Vue instance methods by attaching them to Vue.prototype. 5. A library that provides an API of its own, while at the same time injecting some combination of the above. For example, vue-router **[⬆ Back to Top](#table-of-contents)** 55. ### How to create a plugin? The Plugin is created by exposing an `install` method which takes Vue constructor as a first argument along with options. The structure of VueJS plugin with possible functionality would be as follows, ```javascript MyPlugin.install = function (Vue, options) { // 1. add global method or property Vue.myGlobalMethod = function () { // some logic ... } // 2. add a global asset Vue.directive('my-directive', { bind (el, binding, vnode, oldVnode) { // some logic ... } // ... }) // 3. inject some component options Vue.mixin({ created: function () { // some logic ... } // ... }) // 4. add an instance method Vue.prototype.$myMethod = function (methodOptions) { // some logic ... } } ``` **[⬆ Back to Top](#table-of-contents)** 56. ### How to use a plugin? You can use plugin by passing your plugin to Vue's **use** global method. You need to apply this method before start your app by calling new Vue(). ```javascript // calls `MyPlugin.install(Vue, { someOption: true })` Vue.use(MyPlugin) new Vue({ //... options }) ``` **[⬆ Back to Top](#table-of-contents)** 57. ### What are mixins? Mixin gives us a way to distribute reusable functionalities in Vue components. These reusable functions are merged with existing functions. A mixin object can contain any component options. Let us take an example of mixin with `created` lifecycle which can be shared across components, ```javascript const myMixin = { created(){ console.log("Welcome to Mixins!") } } var app = new Vue({ el: '#root', mixins: [myMixin] }) ``` **Note:** Multiple mixins can be specified in the mixin array of the component. **[⬆ Back to Top](#table-of-contents)** 58. ### What are global mixins? Sometimes there is a need to extend the functionality of Vue or apply an option to all Vue components available in our application. In this case, mixins can be applied globally to affect all components in Vue. These mixins are called as global mixins. Let's take an example of global mixin, ```javascript Vue.mixin({ created(){ console.log("Write global mixins") } }) new Vue({ el: '#app' }) ``` In the above global mixin, the mixin options spread across all components with the console running during the instance creation. These are useful during test, and debugging or third party libraries. At the same time, You need to use these global mixins sparsely and carefully, because it affects every single Vue instance created, including third party components. **[⬆ Back to Top](#table-of-contents)** 59. ### How do you use mixins in CLI? Using Vue CLI, mixins can be specified anywhere in the project folder but preferably within `/src/mixins` for ease of access. Once these mixins are created in a `.js` file and exposed with the `export` keyword, they can be imported in any component with the `import` keyword and their file paths. **[⬆ Back to Top](#table-of-contents)** 60. ### What are the merging strategies in mixins? When a mixin and the component itself contain overlapping options, the options will be merged based on some strategies. 1. The data objects undergo a recursive merge, with the component's data taking priority over mixins in cases of overlapping or conflicts. ```javascript var mixin = { data: function () { return { message: 'Hello, this is a Mixin' } } } new Vue({ mixins: [mixin], data: function () { return { message: 'Hello, this is a Component' } }, created: function () { console.log(this.$data); // => { message: "Hello, this is a Component'" } } }) ``` 2. The Hook functions which are overlapping merged into an array so that all of them will be called. Mixin hooks will be called before the component's own hooks. ```javascript const myMixin = { created(){ console.log("Called from Mixin") } } new Vue({ el: '#root', mixins: [myMixin], created(){ console.log("Called from Component") } }) // Called from Mixin // Called from Component ``` 3. The options that expect object values(such as methods, components and directives) will be merged into the same object. In this case, the component's options will take priority when there are conflicting keys in these objects. ```javascript var mixin = { methods: { firstName: function () { console.log('John') }, contact: function () { console.log('+65 99898987') } } } var vm = new Vue({ mixins: [mixin], methods: { lastName: function () { console.log('Murray') }, contact: function () { console.log('+91 893839389') } } }) vm.firstName() // "John" vm.lastName() // "Murray" vm.contact() // "+91 893839389" ``` **[⬆ Back to Top](#table-of-contents)** 61. ### What are custom options merging strategies? Vue uses the default strategy which overwrites the existing value while custom options are merged. But if you want a custom option merged using custom login then you need to attach a function to `Vue.config.optionMergeStrategies` For the example, the structure of `myOptions` custom option would be as below, ```javascript Vue.config.optionMergeStrategies.myOption = function (toVal, fromVal) { // return mergedVal } ``` Let's take below Vuex 1.0 merging strategy as an advanced example, ```javascript const merge = Vue.config.optionMergeStrategies.computed Vue.config.optionMergeStrategies.vuex = function (toVal, fromVal) { if (!toVal) return fromVal if (!fromVal) return toVal return { getters: merge(toVal.getters, fromVal.getters), state: merge(toVal.state, fromVal.state), actions: merge(toVal.actions, fromVal.actions) } } ``` **[⬆ Back to Top](#table-of-contents)** 62. ### What are custom directives? Custom Directives are tiny commands that you can attach to DOM elements. They are prefixed with v- to let the library know you're using a special bit of markup and to keep syntax consistent. They are typically useful if you need low-level access to an HTML element to control a bit of behavior. Let's create a custom focus directive to provide focus on specific form element during page load time, ```javascript // Register a global custom directive called `v-focus` Vue.directive('focus', { // When the bound element is inserted into the DOM... inserted: function (el) { // Focus the element el.focus() } }) ``` Now you can use v-focus directive on any element as below, ```vue ``` **[⬆ Back to Top](#table-of-contents)** 63. ### How do you register directives locally? You can also register directives locally(apart from globally) using directives option in component as below, ```javascript directives: { focus: { // directive definition inserted: function (el) { el.focus() } } } ``` Now you can use v-focus directive on any element as below, ```vue ``` **[⬆ Back to Top](#table-of-contents)** 64. ### What are the hook functions provided by directives? A directive object can provide several hook functions, 1. bind: This occurs once the directive is attached to the element. 2. inserted: This hook occurs once the element is inserted into the parent DOM. 3. update: This hook is called when the element updates, but children haven't been updated yet. 4. componentUpdated: This hook is called once the component and the children have been updated. 5. unbind: This hook is called only once when the directive is removed. **Note:** There are several arguments that can be passed to the above hooks. **[⬆ Back to Top](#table-of-contents)** 65. ### What are the directive Hook Arguments? All the hooks have `el`, `binding`, and `vnode` as arguments. Along with that, **update** and **componentUpdated** hooks expose `oldVnode`, to differentiate between the older value passed and the newer value. Below are the arguments passed to the hooks, 1. `el`: The element the directive is bound to and it can be used to directly manipulate the DOM. 2. `binding`: An object containing the following properties. 1. `name`: The name of the directive, without the `v-` prefix. 2. `value`: The value passed to the directive. For example in `v-my-directive="1 + 1"`, the value would be 2. 3. `oldValue`: The previous value, only available in update and componentUpdated. It is available whether or not the value has changed. 4. `expression`: The expression of the binding as a string. For example in `v-my-directive="1 + 1"`, the expression would be "1 + 1". 5. `arg`: The argument passed to the directive, if any. For example in v-my-directive:foo, the arg would be " foo". 6. `modifiers`: An object containing modifiers, if any. For example in v-my-directive.foo.bar, the modifiers object would be `{ foo: true, bar: true }`. 3. `vnode`: The virtual node produced by Vue's compiler. 4. `oldVnode`: The previous virtual node, only available in the update and componentUpdated hooks. The arguments can be represented diagrammatically across the hooks as below, ![custom-directives](images/custom-directives.svg) **[⬆ Back to Top](#table-of-contents)** 66. ### How do you pass multiple values to a directive? A directive can take any valid javascript expression. So if you want to pass multiple values then you can pass in a JavaScript object literal. Let's pass object literal to an avatar directive as below ```vue
``` Now let us configure avatar directive globally, ```javascript Vue.directive('avatar', function (el, binding) { console.log(binding.value.width) // 500 console.log(binding.value.height) // 400 console.log(binding.value.url) // path/logo console.log(binding.value.text) // "Iron Man" }) ``` **[⬆ Back to Top](#table-of-contents)** 67. ### What is function shorthand in directive hooks? In few cases, you may want the same behavior on `bind` and `update` hooks irrespective of other hooks. In this situation you can use function shorthand, ```javascript Vue.directive('theme-switcher', function (el, binding) { el.style.backgroundColor = binding.value }) ``` **[⬆ Back to Top](#table-of-contents)** 68. ### What is the benefit of render functions over templates? In VueJS, the templates are very powerful and recommended to build HTML as part of your application. However, some of the special cases like dynamic component creation based on input or slot value can be achieved through render functions. Also, these functions gives the full programmatic power of javascript eco system. **[⬆ Back to Top](#table-of-contents)** 69. ### What is a render function? Render function is a normal function which receives a `createElement` method as it's first argument used to create virtual nodes. Internally Vue.js' templates actually compile down to render functions at build time. Hence templates are just syntactic sugar of render functions. Let's take an example of simple Div markup and corresponding render function. The HTML markup can be written in template tag as below, ```vue ``` and the compiled down or explicit render function would appear as below, ```javascript render: function (createElement) { return createElement('div', { 'class': { 'is-rounded': this.isRounded } }, [ createElement('p', 'Welcome to Vue render functions') ]); } ``` **Note:** The react components are built with render functions in JSX. **[⬆ Back to Top](#table-of-contents)** 70. ### Explain the structure of createElement with arguments? The createElement accepts few arguments to use all the template features. Let us see the basic structure of createElement with possible arguments, ```javascript // @returns {VNode} createElement( // An HTML tag name, component options, or async function resolving to one of these. // Type is {String | Object | Function} // Required. 'div', // A data object corresponding to the attributes you would use in a template. // Type is {Object} // Optional. { // Normal HTML attributes attrs: { id: 'someId' }, // Component props props: { myProp: 'somePropValue' }, // DOM properties domProps: { innerHTML: 'This is some text' }, // Event handlers are nested under `on` on: { click: this.clickHandler }, // Similar to `v-bind:style`, accepting either a string, object, or array of objects. style: { color: 'red', fontSize: '14px' }, // Similar to `v-bind:class`, accepting either a string, object, or array of strings and objects. class: { className1: true, className2: false } // .... }, // Children VNodes, built using `createElement()`, or using strings to get 'text VNodes'. // Type is {String | Array} // Optional. [ 'Learn about createElement arguments.', createElement('h1', 'Headline as a child virtual node'), createElement(MyComponent, { props: { someProp: 'This is a prop value' } }) ] ) ``` See details of the date object in official [doc](https://vuejs.org/v2/guide/render-function.html#The-Data-Object-In-Depth). **[⬆ Back to Top](#table-of-contents)** 71. ### How can you write duplicate virtual nodes in a component? All virtual nodes(VNodes) in the component tree must be unique.i.e, You can't write duplicated nodes in a straightforward way. If you want to duplicate the same element/component many times then you should use factory function. The below render function is invalid where you are trying to duplicate h1 element 3 times, ```javascript render: function (createElement) { var myHeadingVNode = createElement('h1', 'This is a Virtual Node') return createElement('div', [ myHeadingVNode, myHeadingVNode, myHeadingVNode ]) } ``` You can make duplicates with factory function, ```javascript render: function (createElement) { return createElement('div', Array.apply(null, { length: 3 }).map(function () { return createElement('h1', 'This is a Virtual Node') }) ) } ``` **[⬆ Back to Top](#table-of-contents)** 72. ### List down the template equivalents in render functions? VueJS provides proprietary alternatives and plain javascript usage for the template features. Let's list down them in a table for comparison, | Templates | Render function | | ----------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | Conditional and looping directives: v-if and v-for | Use JavaScript's if/else and map concepts | | Two-way binding: v-model | Apply own JS logic with value binding and event binding | | Capture Event modifiers: .passive, .capture, .once and .capture.once or .once.capture | &, !, ~ and ~! | | Event and key modifiers: .stop, .prevent, .self, keys(.enter, .13) and Modifiers Keys(.ctrl, .alt, .shift, .meta) | Use javascript solutions: event.stopPropagation(), event.preventDefault(), if (event.target !== event.currentTarget) return, if (event.keyCode !== 13) return and if (!event.ctrlKey) return | | Slots: slot attributes | Render functions provide this.$slots and this.$scopedSlots instance properties | **[⬆ Back to Top](#table-of-contents)** 73. ### What are functional components? The functional components are just simple functions to create simple components just by passing a context. Every functional component follows two rules, 1. **Stateless:** It doesn't keep any state by itself 2. **Instanceless:** It has no instance, thus no this You need to define `functional: true` to make it functional. Let's take an example of functional components, ```javascript Vue.component('my-component', { functional: true, // Props are optional props: { // ... }, // To compensate for the lack of an instance, // we are now provided a 2nd context argument. render: function (createElement, context) { // ... } }) ``` **Note:** The functional components are quite popular in React community too. **[⬆ Back to Top](#table-of-contents)** 74. ### What are the similarities between VueJS and ReactJS? Even though ReactJS and VueJS are two different frameworks there are few similarities(apart from the common goal of utilized in interface design) between them. 1. Both frameworks are based on the **Virtual DOM** model 2. They provide features such Component-based structure and reactivity 3. They are intended for working with the root library, while all the additional tasks are transferred to other libraries(routing, state management etc). **[⬆ Back to Top](#table-of-contents)** 75. ### What is the difference between VueJS and ReactJS? Even though VueJS and ReactJS share few common features there are many difference between them. Let's list down them in a table format. | Feature | VueJS | ReactJS | | --------------------- | ------------------------------------ | -------------------------------------------------- | | Type | JavaScript MVVM Framework | JavaScript Library | | Platform | Primarily focused on web development | Both Web and Native | | Learning Curve | Easy to learn the framework | A steep learning curve and requires deep knowledge | | Simplicity | Vue is simpler than React | React is more complex than Vue | | Bootstrap Application | Vue-cli | CRA (Create React App) | **[⬆ Back to Top](#table-of-contents)** 76. ### What are the advantages of VueJS over ReactJS? Vue has the following advantages over React 1. Vue is smaller and faster 2. The convenient templates ease the process of developing 3. It has simpler javascript syntax without learning JSX **[⬆ Back to Top](#table-of-contents)** 77. ### What are the advantages of ReactJS over VueJS? React has the following advantages over Vue 1. ReactJS gives more flexibility in large apps developing 2. Easy to test 3. Well-suited for mobile apps creation 4. The eco system is quite big and well matured. **[⬆ Back to Top](#table-of-contents)** 78. ### What are the differences between VueJS and Angular? The the syntax of Vue and Angular is common at some points because Angular is the basis for VueJS development in the beginning. But there are many differences between VueJS and Angular as listed, | Feature | VueJS | Angular | | --------------- | ------------------------------------------- | ------------------------------------------------------------------------ | | Complexity | Easy to learn, simple API and design | The framework is bit huge and need some learning curve on typescript etc | | Binding of Data | One-way binding | Two-way binding | | Learning Curve | Easy to learn the framework | A steep learning curve and requires deep knowledge | | Founders | Created by Former Google Employee | Powered by Google | | Initial Release | February 2014 | September 2016 | | Model | Based on Virtual DOM(Document Object Model) | Based on MVC(Model-View-Controller) | **[⬆ Back to Top](#table-of-contents)** 79. ### What are dynamic components? The dynamic component will allow you to dynamically switch between multiple components without updating the route of the application and even retaining the state of the component when switching back to the initial component. It works by using `` tag with `v-bind:is` attribute which accept dynamic component. Let's create a dynamic component vue instance to switch between different pages of a website, ```javascript new Vue({ el: '#app', data: { currentPage: 'home' }, components: { home: { template: "

Home

" }, about: { template: "

About

" }, contact: { template: "

Contact

" } } }) ``` Now you can use the dynamic component which holds the current page, ```html
``` The component will be unmounted when it is switched away but it is possible to force the inactive component alive using `` component **Note:** The value of `:is` attribute can be either name of the registered component or the actual imported object itself. **[⬆ Back to Top](#table-of-contents)** 80. ### What is the purpose of keep alive tag? Keep-alive tag is an abstract component used to preserve component state or avoid re-rendering. When you wrapped < keep-alive> tag around a dynamic component, it caches the inactive component instances without destroying them. Let's see the example usage of it, ```javascript ``` When there are multiple conditional children, it requires that only one child is rendered at a time. ```javascript ``` **Note:** Remember that keep-alive tag doesn't render a DOM element itself, and doesn't show up in the component parent chain. **[⬆ Back to Top](#table-of-contents)** 81. ### What are async components? In large applications, we may need to divide the app into smaller chunks and only load a component from the server when it's needed. To make this happen, Vue allows you to define your component as a factory function that asynchronously resolves your component definition. These components are known as async component. Let's see an example of async component using webpack code-splitting feature, ```javascript Vue.component('async-webpack-example', function (resolve, reject) { // Webpack automatically split your built code into bundles which are loaded over Ajax requests. require(['./my-async-component'], resolve) }) ``` Vue will only trigger the factory function when the component needs to be rendered and will cache the result for future re-renders. **[⬆ Back to Top](#table-of-contents)** 82. ### What is the structure of async component factory? Async component factory is useful to resolve the component asynchronously. The async component factory can return an object of the below format. ```javascript const AsyncComponent = () => ({ // The component to load (should be a Promise) component: import('./MyComponent.vue'), // A component to use while the async component is loading loading: LoadingComponent, // A component to use if the load fails error: ErrorComponent, // Delay before showing the loading component. Default: 200ms. delay: 200, // The error component will be displayed if a timeout is // provided and exceeded. Default: Infinity. timeout: 3000 }) ``` **[⬆ Back to Top](#table-of-contents)** 83. ### What are inline templates? If you keep an `inline-template` on a child component then it will use its inner content as a template instead of treating as reusable independent content. ```javascript

Inline templates

Treated as component component owne content

``` **Note:** Even though this inline-templates gives more flexibility for template authoring, it is recommended to define template using template property or