• +91 9723535972
  • info@interviewmaterial.com

Vue JS Interview Questions and Answers

Vue JS Interview Questions and Answers

Question - 61 : - What are key modifiers?

Answer - 61 : -

Vue supports key modifiers on v-on for handling keyboard events. Let's take an example of keyup event with enter keycode.

Remembering all the key codes is really difficult. It supports the full list of key codes aliases

  • .enter
  • .tab
  • .delete (captures both “Delete” and “Backspace” keys)
  • .esc
  • .space
  • .up
  • .down
  • .left
  • .right
Now the above keyup code snippet can be written with aliases as follows,


Question - 62 : - How do you define custom key modifier aliases?

Answer - 62 : -

You can define custom key modifier aliases via the global config.keyCodes. There are few guidelines for the properties

You can't use camelCase. Instead you can use kebab-case with double quotation marks
You can define multiple values in an array format
Vue.config.keyCodes = {
  f1: 112,
  "media-play-pause": 179,
  down: [40, 87]
}

Question - 63 : - What are the supported System Modifier Keys?

Answer - 63 : -

Vue supports below modifiers to trigger mouse or keyboard event listeners when the corresponding key is pressed,

  • .ctrl
  • .alt
  • .shift
  • .meta
Lets take an example of control modifier with click event,

Do something

Question - 64 : - What are the supported Mouse Button Modifiers?

Answer - 64 : -

Vue supports below mouse button modifiers

  • .left
  • .right
  • .middle
For example, the usage of .right modifier as below

 
   v-if="button === 'right'"
   v-on:mousedown.right="increment"
   v-on:mousedown.left="decrement"
 />

Question - 65 : - How do you implement two-way binding?

Answer - 65 : -

You can use the v-model directive to create two-way data bindings on form input, textarea, and select elements.

Lets take an example of it using input component,

The message is: {{ message }}

Remember, v-model will ignore the initial value, checked or selected attributes found on any form elements. So it always use the Vue instance data as the source of truth.

Question - 66 : - What are the supported modifiers on model?

Answer - 66 : -

There are three modifiers supported for v-model directive.

1. lazy: By default, v-model syncs the input with the data after each input event. You can add the lazy modifier to instead sync after change events.

2. number: If you want user input to be automatically typecast as a number, you can add the number modifier to your v-model. Even with type="number", the value of HTML input elements always returns a string. So, this typecast modifier is required.

3. trim: If you want whitespace from user input to be trimmed automatically, you can add the trim modifier to your v-model.


Question - 67 : - What are components and give an example?

Answer - 67 : -

Components are reusable Vue instances with a name. They accept the same options as new Vue, such as data, computed, watch, methods, and lifecycle hooks(except few root-specific options like el).

Lets take an example of counter component,

// Define a new component called button-counter
Vue.component('button-counter', {
  template: ''
  data: function () {
    return {
      count: 0
    }
  },
})
Let's use this component inside a root Vue instance created with new Vue

 

var vm = new Vue({ el: '#app' });

Question - 68 : - What are props?

Answer - 68 : -

Props are custom attributes you can register on a component. When a value is passed to a prop attribute, it becomes a property on that component instance. You can pass those list of values as props option and use them as similar to data variables in template.

Vue.component('todo-item', {
  props: ['title'],
  template: '

{{ title }}

'
})
Once the props are registered, you can pass them as custom attributes.


Question - 69 : - When component needs a single root element?

Answer - 69 : -

In VueJS 2.x, every component must have a single root element when template has more than one element. In this case, you need to wrap the elements with a parent element.

Otherwise there will an error throwing, saying that "Component template should contain exactly one root element...".

Whereas in 3.x, components now can have multiple root nodes. This way of adding multiple root nodes is called as fragments.


Question - 70 : - What are slots?

Answer - 70 : -

Vue implements a content distribution API using the element to serve as distribution outlets for content created after the current Web Components spec draft.

Let's create an alert component with slots for content insertion,

Vue.component('alert', {
  template: `
   
      Error!
     
   
  `
})
Now you can insert dynamic content as below,

  There is an issue with in application.


NCERT Solutions

 

Share your email for latest updates

Name:
Email:

Our partners