The operating environment of this tutorial: Windows 7 system, vue 2.9.6 version, DELL G3 computer. 1. What are components?Recall the previous definition of components: A component is a development model that abstracts various graphic and non-graphic logic into a unified concept (component). In Vue, each .vue file can be regarded as a component. Advantages of components
2. What is a plug-in?Plugins are often used to add global functionality to Vue. There are no strict limits on the scope of functionality of plugins - generally they fall into the following categories:
3. The difference between the twoThe differences between the two are mainly reflected in the following aspects:
Writing format Writing Components There are many ways to write a component. The most common one is the vue single file format. Each .vue file can be regarded as a component. vue file standard format <template> </template> <script> export default{ ... } </script> <style> </style> We can also write a component through the template attribute. If the component content is large, we can define the template component content externally. If the component content is not large, we can write it directly on the template attribute. <template id="testComponent"> // Component display content <div>component!</div> </template> Vue.component('componentA',{ template: '#testComponent' template: `<div>component</div>` // This format can be used for components with little content}) Writing plugins Vue plugin implementations should expose an install method. The first parameter of this method is the Vue constructor, and the second parameter is an optional options object. MyPlugin.install = function (Vue, options) { // 1\. Add global method or property Vue.myGlobalMethod = function () { // Logic... } // 2\. Add global resources Vue.directive('my-directive', { bind (el, binding, vnode, oldVnode) { // Logic... } ... }) // 3\. Inject component options Vue.mixin({ created: function () { // Logic... } ... }) // 4\. Add instance method Vue.prototype.$myMethod = function (methodOptions) { // Logic... } } Registration Form Component Registration Vue component registration is mainly divided into global registration and local registration Global registration is done through the Vue.component method. The first parameter is the name of the component and the second parameter is the passed in configuration item. Vue.component('my-component-name', { /* ... */ }) Local registration only requires registering a component through the components property where it is used. const component1 = {...} // define a component export default { components:{ component1 // local registration}} Plugin Registration The plugin is registered (installed) via Vue.use(). The first parameter is the name of the plugin and the second parameter is an optional configuration item. Vue.use(plugin name,{ /* ... */} ) Note that: When registering a plugin, you need to do it before calling new Vue() to start the application. Vue.use will automatically prevent multiple registrations of the same plugin and will only register it once Usage scenarios The details have been described in the section on what plugins are, so let’s summarize them here. Component is used to form the business module of your App. Its target is App.vue Plugins are functional modules used to enhance your technology stack. Their target is Vue itself. Simply put, a plugin is an enhancement or supplement to Vue's functionality. This concludes this article about the differences and usage summary of plugins and components in Vue. For more information about the differences between plugins and components in Vue, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Solution to the ineffectiveness of flex layout width in css3
>>: The browser caches the relevant http headers to minimize the number of http requests
nginx installation Ensure that the virtual machin...
This article shares the specific code of jQuery t...
This article example shares the specific code of ...
This article uses an example to describe the crea...
wangEditor is a web rich text editor developed ba...
Table of contents 1. Example scenario 1.1. Set th...
Table of contents Prerequisites useEffect commitB...
From the backend to the front end, what a tragedy....
The effect to be achieved In many cases, we will ...
Simple application deployment 1. Directory struct...
I just joined a new company recently. After getti...
Stored Procedures 1. Create a stored procedure an...
Quickly modify the table structure of a MySQL tab...
Table of contents 1. Mini Program Subcontracting ...
1. Download the MySQL 5.7 installation package fr...