YouTip LogoYouTip

Vue3 Components

Each Vue component is an independent Vue instance with its own template, data, methods, and lifecycle hooks, allowing components to self-containedly define and manage their functionality and styles. Components are one of the most powerful features of Vue.js. Components can extend HTML elements, encapsulate reusable code, and help you split the user interface into independent and reusable parts. Each Vue component is an independent Vue instance with its own template, data, methods, and lifecycle hooks, allowing components to self-containedly define and manage their functionality and styles. ### Features and Advantages of Vue Components * **Reusability**: Components can be used multiple times in different places, improving code reusability and maintainability. * **Encapsulation**: Each component is an independent scope that can encapsulate its own state and logic, avoiding global pollution. * **Composability**: You can build complex interfaces by combining multiple smaller components. * **Reactivity**: Component data is reactively bound to the view, and the view updates automatically when data changes. * **Modularization**: Supports modular development and can be built and managed using modern frontend toolchains. The component system allows us to build large applications from independent reusable components. Almost any type of application's interface can be abstracted as a component tree: !(#) Every Vue application is created using the createApp function. The options passed to createApp are used to configure the root component. When we mount the application, this component is used as the starting point for rendering. An application needs to be mounted to a DOM element. In the following example, we mount the Vue application to
, so we should pass #app: const RootComponent = { /* options */ } const app = Vue.createApp(RootComponent) const vm = app.mount('#app') To register a global component, the syntax format is as follows: const app = Vue.createApp({...}) app.component('my-component-name', { /* ... */}) **my-component-name** is the component name, and **/* ... */** is the configuration options. After registration, we can call the component in the following way: A simple Vue component example: ## Global Component Example Register a simple global component named tutorial and use it:
// Create a Vue application const app = Vue.createApp({}) // Define a new global component named tutorial app.component('tutorial', { template: '

Custom Component!

' }) app.mount('#app') [Try it Β»](#) Next, let's register another button-counter component, which will increment the counter by 1 after each click: ## Example // Create a Vue application const app = Vue.createApp({}) // Define a new global component named button-counter app.component('button-counter',{ data(){ return{ count:0 } }, template: ` ` }) app.mount('#app') [Try it Β»](#) **Note**: The ` in template is a backtick, not a single quote '. ### Component Reusability You can reuse components any number of times: ## Example
[Try it Β»](#) ### Global Components In the examples above, our components are only registered globally using component. Globally registered components can be used in the templates of subsequently created app instances, including in all sub-component templates in the root instance component tree. ## Global Component Example Register a simple global component named tutorial and use it:
// Create a Vue application const app = Vue.createApp({}) // Define a new global component named tutorial app.component('tutorial', { template: '

Custom Component!

' }) app.mount('#app') [Try it Β»](#) ### Local Components Global registration is often not ideal. For example, if you use a build system like webpack, global registration of all components means that even if you no longer use a component, it will still be included in your final build. This causes unnecessary increase in JavaScript that users have to download. In these cases, you can define components through ordinary JavaScript objects: const ComponentA = { /* ... */} const ComponentB = { /* ... */} const ComponentC = { /* ... */} Then define the components you want to
← Vue3 WatchVue3 V If β†’