CoderFunda
  • Home
  • About us
    • Contact Us
    • Disclaimer
    • Privacy Policy
    • About us
  • Home
  • Php
  • HTML
  • CSS
  • JavaScript
    • JavaScript
    • Jquery
    • JqueryUI
    • Stock
  • SQL
  • Vue.Js
  • Python
  • Wordpress
  • C++
    • C++
    • C
  • Laravel
    • Laravel
      • Overview
      • Namespaces
      • Middleware
      • Routing
      • Configuration
      • Application Structure
      • Installation
    • Overview
  • DBMS
    • DBMS
      • PL/SQL
      • SQLite
      • MongoDB
      • Cassandra
      • MySQL
      • Oracle
      • CouchDB
      • Neo4j
      • DB2
      • Quiz
    • Overview
  • Entertainment
    • TV Series Update
    • Movie Review
    • Movie Review
  • More
    • Vue. Js
    • Php Question
    • Php Interview Question
    • Laravel Interview Question
    • SQL Interview Question
    • IAS Interview Question
    • PCS Interview Question
    • Technology
    • Other

28 December, 2020

Vue.js Component

 Programing Coderfunda     December 28, 2020     Vue.js Tutorial     No comments   

Vue.js Component

Vue.js components are one of Vue.js' most important features and used to create custom elements that can be reused in HTML. Components are the reusable Vue.js instances with a name. We can use a component as a custom element inside a root Vue.js instance.

Following is the syntax to create a component.

Syntax:

  1. Vue.component('nameofthecomponent',{ // options});  

Let's create a component to understand it better how components work with Vue.js. See the following example:

Index.html file:

  1. <html>  
  2.    <head>  
  3.       <title>Vue.js Component</title>  
  4.       <link rel="stylesheet" href="index.css">  
  5.         <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>  
  6.     </head>  
  7.     <body>  
  8.       <div id = "component_test1">  
  9.          <testcomponent></testcomponent>  
  10.       </div>  
  11.       <div id = "component_test2">  
  12.          <testcomponent></testcomponent>  
  13.       </div>  
  14.       <script src="index.js"></script>  
  15.    </body>  
  16. </html>  

Index.js file:

  1. Vue.component('testcomponent',{  
  2.    template : '<div><h1>This is a component example</h1></div>'  
  3. });  
  4. var vm = new Vue({  
  5.    el: '#component_test1'  
  6. });  
  7. var vm1 = new Vue({  
  8.    el: '#component_test2'  
  9. });  

Let's use a simple CSS file to make the output more attractive.

Index.css file:

  1. html, body {  
  2.     margin: 5px;  
  3.     padding: 0;  
  4. }  

After the execution of the program, you will see the following output:

Output:

This is a component example
This is a component example
Vue.js Component

Example Explanation

In the above example, we have created two div with id component_test1 and component_test2 in the Index.html file. Once a component is created, the name of the component becomes the custom element and we can use this custom element in the Vue instance element created, i.e. inside the div with ids component_test1 and component_test2. Here, we have used a test component as the name of the component and the same name is used as the custom element inside the divs.

  1. <div id = "component_test1">  
  2.    <testcomponent></testcomponent>  
  3. </div>  
  4. <div id = "component_test2">  
  5.    <testcomponent></testcomponent>  
  6. </div>  

In the Index .js file, we have created two Vue.js instances with the respective div ids.

  1. var vm = new Vue({  
  2.    el: '#component_test1'  
  3. });  
  4. var vm1 = new Vue({  
  5.    el: '#component_test2'  

We have created a common component to use with both the view instances.

  1. Vue.component('testcomponent',{  
  2.    template : '<div><h1>This is a component example</h1></div>'   

Local registration of Components

We can directly make the components a part of vue.js instance by using the following code in Index.js file. This method is called local registration. Here, the components will be a part of only the created vue instance.

  1. var vm = new Vue({  
  2.    el: '#component_test1',  
  3.    components:{  
  4.       'testcomponent': {  
  5.         template : '<div><h1>This is a component example</h1></div>'   
  6.       }  
  7.    }  
  8. });  

After the execution of the program, you will see the following output:

Output:

Vue.js Component

Vue.js components with more options

We can add some more options such as data and methods to Vue.js components. It is same as we have added data and methods to Vue.js instance. See the following example:

Index.html file:

  1. <html>  
  2.    <head>  
  3.       <title>Vue.js Component</title>  
  4.       <link rel="stylesheet" href="index.css">  
  5.         <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>  
  6.     </head>  
  7.     <body>  
  8.       <div id = "component_test1">  
  9.          <testcomponent></testcomponent>  
  10.       </div>  
  11.       <div id = "component_test2">  
  12.          <testcomponent></testcomponent>  
  13.       </div>  
  14.       <script src="index.js"></script>  
  15.    </body>  
  16. </html>  

Index.js file:

  1. Vue.component('testcomponent',{  
  2.    template : '<div v-on:mouseover = "changename()" v-on:mouseout = "originalname();"><h1>This Custom Component is created by <span id = "name">{{name}}</span></h1></div>',  
  3.    data: function() {  
  4.       return {  
  5.          name : "Panda"  
  6.       }  
  7.    },  
  8.    methods:{  
  9.       changename : function() {  
  10.          this.name = "Alex";  
  11.       },  
  12.       originalname: function() {  
  13.          this.name = "Panda";  
  14.       }  
  15.    }  
  16. });  
  17. var vm = new Vue({  
  18.    el: '#component_test1'  
  19. });  
  20. var vm1 = new Vue({  
  21.    el: '#component_test2'  
  22. });  

In the above Index.js file, we have added a data in the form of a function that returns an object. The object has a name property, which has assigned the value 'Panda'. See the following template used in the example.

  1. template : '<div v-on:mouseover = "changename()" v-on:mouseout = "originalname();"><h1>This Custom Component is created by <span id = "name">{{name}}</span></h1></div>',  
  2.    data: function() {  
  3.       return {  
  4.          name : "Panda"  
  5.       }  

Here, we are using data as a function in components and we also use its properties the same way as we use with direct Vue instance. Here, we have used two methods, changename and originalname. In changename, we are changing the name property, and in originalname we are resetting it back to the original name.

We have also added two events on the div, mouseover and mouseout. Here, mouseover event is used to call changename method and mouseout event is used to call originalname method.

Now, execute the above program to see the output. After the execution of the program, you will see the following output:

Output:

Vue.js Component

You can see that the output displays the assigned name "Panda" which we have set in the data property in the Index.js file. We have also assigned a mouseover and a mouseout event on the div. So on mouseover, you will see that the name of the components are changed to Alex, and when mouseout, it will remain the same as Panda.

Vue.js Component

Vue.js Dynamic Components

Dynamic component means, a component which location in the application is not defined at buildtime. We don't define it in any Vue.js template. Instead of this, the dynamic component is instantiated and placed in the application at runtime. In Vue.js, a dynamic component is created using the keyword <component></component> and it is bound using a property. Let's take an example to understand it clearly.

Index.html file:

  1. <html>  
  2.    <head>  
  3.       <title>Vue.js Component</title>  
  4.       <link rel="stylesheet" href="index.css">  
  5.         <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>  
  6.     </head>  
  7.     <body>  
  8.       <div id = "databinding">  
  9.          <component v-bind:is = "view"></component>  
  10.       </div>  
  11.       <script type = "text/javascript">  
  12.          var vm = new Vue({  
  13.             el: '#databinding',  
  14.             data: {  
  15.                view: 'component1'  
  16.             },  
  17.             components: {  
  18.                'component1': {  
  19.                  template: '<div><span style = "font-size:25;color:Blue;">This is a Dynamic Component Example</span></div>'  
  20.                }  
  21.             }  
  22.          });  
  23.       </script>  
  24.       <script src="index.js"></script>  
  25.    </body>  
  26. </html>  

Index.js file:

  1. var vm = new Vue({  
  2.    el: '#databinding',  
  3.    data: {  
  4.       view: 'component1'  
  5.    },  
  6.    components: {  
  7.       'component1': {  
  8.          template: '<div><span style = "font-size:25;color:Blue;">This is a Dynamic Component Example</span></div>'  
  9.       }  
  10.    }  
  11. });  

Let's use a simple CSS file to make the output more attractive.

Index.css file:

  1. html, body {  
  2.     margin: 5px;  
  3.     padding: 0;  
  4. }  

After the execution of the program, you will see the following output:

Output:

This is a Dynamic Component Example

Vue.js Component

Example explanation

In the above example, you can see that the Dynamic component is created by using the following syntax.

  1. <component v-bind:is = "view"></component>  

Here, it has v-bind:is = "view", and a value view is assigned to it. View is defined in the Vue instance as follows.

  1. var vm = new Vue({  
  2.    el: '#databinding',  
  3.    data: {  
  4.       view: 'component1'  
  5.    },  
  6.    components: {  
  7.       'component1': {  
  8.          template: '<div><span style = "font-size:25;color:red;">Dynamic Component</span></div>'  
  9.       }  
  10.    }  
  11. });  

After the execution, you can see that the template displays that "This is a Dynamic Component Example" in the browser.

By Source : javatoint

  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to XShare to Facebook

Related Posts:

  • VueJS Directives Directives are instruction for VueJS to do things in a certain way. We have already seen directives such as v-if, v-show, v-else, v-for, v-bind … Read More
  • VueJS - Transition and AnimationIn this chapter, we will discuss the transition and animation features available in VueJS.TransitionVueJS provides various ways to apply transition to… Read More
  • VueJS - RenderingIn this chapter, we will learn about conditional rendering and list rendering. In conditional rendering, we will discuss about using if, if-else, if-e… Read More
  • VueJS RoutingVueJS does not have a built-in router feauture. We need to follow some additional steps to install it.Direct Download from CDNThe latest version of vu… Read More
  • VueJS MixinsMixins are basically to be used with components. They share reusable code among components. When a component uses mixin, all options of mixin become a… Read More
Newer Post Older Post Home

0 comments:

Post a Comment

Thanks

Meta

Popular Posts

  • Show page numbers as navigation in Laravel pagination
      Answer Sorted by:                                                Highest score (default)                                                  ...
  • Bootstrap - Code
    Bootstrap - Code Bootstrap allows you to display code with two different key ways − The first is the <code> tag. If you are going to ...
  • Reasons to use WordPress
      Reasons to use WordPress There are many reasons to use WordPress in today's scenario as it provides a great help to its users in all r...
  • Inertia and React or Vue
    Hi just checking your thoughts on whether to learn React or Vue, I want to learn React as it may be better to find work and it has a larger ...
  • Laravel Passwordless Login
      Laravel Passwordless login is a package by   Ed Grosvenor   that provides a simple, safe, magic login link generator for Laravel apps: Thi...

Categories

  • Ajax (26)
  • Bootstrap (30)
  • DBMS (42)
  • HTML (12)
  • HTML5 (45)
  • JavaScript (10)
  • Jquery (34)
  • Jquery UI (2)
  • JqueryUI (32)
  • Laravel (1017)
  • Laravel Tutorials (23)
  • Laravel-Question (6)
  • Magento (9)
  • Magento 2 (95)
  • MariaDB (1)
  • MySql Tutorial (2)
  • PHP-Interview-Questions (3)
  • Php Question (13)
  • Python (36)
  • RDBMS (13)
  • SQL Tutorial (79)
  • Vue.js Tutorial (69)
  • Wordpress (150)
  • Wordpress Theme (3)
  • codeigniter (108)
  • oops (4)
  • php (853)

Social Media Links

  • Follow on Twitter
  • Like on Facebook
  • Subscribe on Youtube
  • Follow on Instagram

Pages

  • Home
  • Contact Us
  • Privacy Policy
  • About us

Blog Archive

  • July (4)
  • September (100)
  • August (50)
  • July (56)
  • June (46)
  • May (59)
  • April (50)
  • March (60)
  • February (42)
  • January (53)
  • December (58)
  • November (61)
  • October (39)
  • September (36)
  • August (36)
  • July (34)
  • June (34)
  • May (36)
  • April (29)
  • March (82)
  • February (1)
  • January (8)
  • December (14)
  • November (41)
  • October (13)
  • September (5)
  • August (48)
  • July (9)
  • June (6)
  • May (119)
  • April (259)
  • March (122)
  • February (368)
  • January (33)
  • October (2)
  • July (11)
  • June (29)
  • May (25)
  • April (168)
  • March (93)
  • February (60)
  • January (28)
  • December (195)
  • November (24)
  • October (40)
  • September (55)
  • August (6)
  • July (48)
  • May (2)
  • January (2)
  • July (6)
  • June (6)
  • February (17)
  • January (69)
  • December (122)
  • November (56)
  • October (92)
  • September (76)
  • August (6)

  • Sitaare Zameen Par Full Movie Review - 7/7/2025
  • Step-by-step Vue.js Tutorial Beginner to Master - 7/7/2025
  • Tailwindcss best practices for responsive design - 7/1/2025
  • Tailwind CSS Tutorial (Beginner to Master) - 7/1/2025
  • Failed to install 'cordova-plugin-firebase': CordovaError: Uh oh - 9/21/2024

Laravel News

  • Fix Static Analysis Issues Automatically with CodeKudu - 11/2/2025
  • Inspect Composer and NPM Dependency Changes With Whatsdiff - 10/31/2025
  • Define LLM JSON Schemas in Laravel With Forerunner - 10/30/2025
  • MongoDB Transactions in Laravel - 10/29/2025
  • Concurrency Control in Laravel 12.36, Inertia View Transitions - 10/29/2025

Copyright © 2025 CoderFunda | Powered by Blogger
Design by Coderfunda | Blogger Theme by Coderfunda | Distributed By Coderfunda