What are components? What is a plug-in? The following article will take you to understand the plug-ins and components inVue, and talk about the differences between plug-ins and components. I hope it will be helpful to everyone!
Review the previous definition of a component:
A component is a combination of graphics, Various non-graphical logics are abstracted into a unified concept (component) to implement the development model. InVue
, each.vue
file can be regarded as a component. (Learning video sharing:vue video tutorial)
Advantages of components
Plug-ins are usually used to add global functions toVue
. There are no strict restrictions on the functional scope of plug-ins - generally there are the following types:
vue-custom-element
vue-touch
vue-router
Vue
instance methods by adding them toVue.prototype
.API
while providing one or more of the functions mentioned above. Such asvue-router
The difference between the two is mainly reflected in the following aspects:
There are many ways to write a component. Our most common one is the format ofvue
single file, each.vue
We can all regard files as a component
vue
File standard format
We can also write a component through thetemplate
attribute, If there is a lot of component content, we can define thetemplate
component content externally. If the component content is not much, we can write it directly on thetemplate
attribute
// 组件显示的内容component!Vue.component('componentA',{ template: '#testComponent' template: `component` // 组件内容少可以通过这种形式 })
vue
The implementation of the plug-in should expose aninstall
method. The first parameter of this method is theVue
constructor, and the second parameter is an optional options object
MyPlugin.install = function (Vue, options) { // 1. 添加全局方法或 property Vue.myGlobalMethod = function () { // 逻辑... } // 2. 添加全局资源 Vue.directive('my-directive', { bind (el, binding, vnode, oldVnode) { // 逻辑... } ... }) // 3. 注入组件选项 Vue.mixin({ created: function () { // 逻辑... } ... }) // 4. 添加实例方法 Vue.prototype.$myMethod = function (methodOptions) { // 逻辑... } }
vue
Component registration is mainly divided into global registration and local registration
Global registration through theVue.component
method , the first parameter is the name of the component, and the second parameter is the incoming configuration item
Vue.component('my-component-name', { /* ... */ })
1\
Local registration only needs to be done where it is usedcomponents
Attribute registration of a component
const component1 = {...} // 定义一个组件 export default { components:{ component1 // 局部注册 } }
Plug-in registration is registered (installed) throughVue.use()
, The first parameter is the name of the plug-in, and the second parameter is the optional configuration item
Vue.use(插件名字,{ /* ... */} )
Note:
When registering the plug-in, you need to callnew Vue( )
Completed before starting the application
Vue.use
will automatically prevent multiple registrations of the same plug-in, and will only register it once
The specifics have been explained in the chapter about what a plug-in is. Here is a summary
Component(Component)
is used to form yourApp
Business module, its goal isApp.vue
Plug-in(Plugin)
is a functional module used to enhance your technology stack, its goal isVue
itself
Simply put, plug-ins refer to enhancements or supplements to the functions ofVue
(Learning video sharing:web Front-end development,Basic programming video)
The above is the detailed content of Let's briefly analyze the plug-ins and components in Vue and talk about their differences!. For more information, please follow other related articles on the PHP Chinese website!