1.

What are Components in Vue.js? Show how to create one inside a Vue instance.

Answer»

In Vue.js component 

  • ONE of the essential features of VueJS is that it creates custom ELEMENTS
  • It is a reusable Vue instance with a name that can be reused as many times as we want. 

Steps to create a Vue component called ‘ComponentOne’ INSIDE the Vue instance. 

Step 1: Create the component by defining a COMMON element of the UI(User interface) to be displayed. Save the file with name ComponentFirst.vue 

<template&GT;  <div><h1>This is coming from component First</h1></div>  </template> 

The component consists of an h1 tag nested in div tag to print ‘This is coming from component First’ at the size of heading one. 

Step 2: Define the vue instance in which the component is imported (included) 

<script>  import ComponentOne from './ComponentFirst.vue'  export default {    components: {      ComponentFirst  }  </script>    <template>  < h1 >Here is the reused component!</h1>  <ComponentFirst />  <ComponentFirst />  </template> 

>> Output 

  • Here is the reused component! 
  • This is coming from component First 
  • This is coming from component First 


Discussion

No Comment Found

Related InterviewSolutions