Home > Article > Web Front-end > What is used to declare a component in vue
There are three ways to declare components in Vue: register globally through the Vue.component method. When using a registered component in a template, the component name should be named using kebab-case. Register components locally in the current component via the components option.
Methods to declare components in Vue
1. Component registration
Components can be registered using the Vue.component(name, options)
method. Among them, name
is the component name, options
is the component configuration object, including template, data, methods and other attributes.
<code class="javascript">// 注册组件 Vue.component('my-component', { template: '<div>这是我的组件</div>' });</code>
2. The registered component declared in the template
can be used through the template tag, where the component name is named with kebab-case.
<code class="javascript">// 模板中使用组件 <template> <my-component></my-component> </template></code>
3. Partial registration
Components can also be partially registered and only used in the current component. Components can be registered into the current component using the components
option.
<code class="javascript">// 当前组件中局部注册组件 export default { components: { 'my-component': { template: '<div>这是我的局部组件</div>' } } };</code>
Other declaration methods
In addition to the above methods, components can also be declared in the following ways:
extend
method to extend the Vue constructor. The above is the detailed content of What is used to declare a component in vue. For more information, please follow other related articles on the PHP Chinese website!