Intermediate
How do you create a Vue component?
A Vue component is a reusable instance with its own data, methods, and lifecycle. It helps in breaking down the UI into smaller, manageable parts. To create a component, you can use the Vue.component method or the newer SFC (Single File Component) structure.
Follow these steps to create a simple component:
- Define the component: Use the Vue.component method.
- Template: Create a template that outlines the HTML structure.
- Script: Define the data and methods.
Here’s an example of a simple Vue component:
Vue.component('my-component', {
template: '<div>Hello, {{ name }}!</div>',
data: function() {
return { name: 'Vue' };
}
});