• +91 9723535972
  • info@interviewmaterial.com

Vue JS Interview Questions and Answers

Vue JS Interview Questions and Answers

Question - 71 : - What are possible prop types?

Answer - 71 : -

You can declare props with type or without type. But it is recommended to have prop types because it provides the documentation for the component and warns the developer for any incorrect data type being assigned.

props: {
  name: String,
  age: Number,
  isAuthenticated: Boolean,
  phoneNumbers: Array,
  address: Object
}
As mentioned in the above code snippet, you can list props as an object, where the properties’ names and values contain the prop names and types, respectively.

Question - 72 : - What is the data flow followed by props?

Answer - 72 : -

All props follows a one-way-down binding between the child property and the parent one. i.e, When the parent property is updated then that latest prop value will be passed down to the child, but not the otherway(child to parent) around. The child component should not mutate the prop otherwise it throws a warning in the console. The possible mutation cases can be solved as below,

When you try to use parent prop as initial value for child property:

In this case you can define a local property in child component and assign parent value as initial value

props: ['defaultUser'],
data: function () {
  return {
    username: this.defaultUser
  }
}
When you try to transform the parent prop:

You can define a computed property using the prop’s value,

props: ['environment'],
computed: {
  localEnvironment: function () {
    return this.environment.trim().toUpperCase()
  }
}

Question - 73 : - What are non prop attributes?

Answer - 73 : -

A non-prop attribute is an attribute that is passed to a component, but does not have a corresponding prop defined.

For example, If you are using a 3rd-party custom-input component that requires a data-tooltip attribute on the input then you can add this attribute to component instance,

If you try to pass the props from parent component the child props with the same names will be overridden. But props like class and style are exception to this, these values will be merged in the child component.



Question - 74 : - How can you handle Events in Vue.js?

Answer - 74 : -

See the following example which demonstrates how to handle Events in Vue.js:

HTML Code:

  
  Name:   
 
div>   
JS Code:

var myViewModel = new Vue({  
  el: '#app',  
  data: my Model,  
  // A click handler inside methods  
  methods: {  
    ClickHandler: function(e) {  
      alert("Hello " + this.name);  
    }  
  }  
 });  

Question - 75 : - What are the most commonly used Directives in Vue.js? / What do you understand by the conditional directives in Vue.js?

Answer - 75 : -

There are a set of directives in Vue.js used to show or hide elements according to the conditions. These directives are also known as conditional directives.

  • v-if
  • v-else
  • v-else-if
  • v-show
  • v-model
  • v-on
v-if directive: The v-if directive is used to add or remove the DOM elements based on the given expression. For example, the below button will not show if isLoggedIn if you set it to false.

  
The v-if directive also facilitates you to control multiple elements with a single v-if statement by wrapping all the elements in a