How to write vue plug-in
This time, the editor will teach you how to write a vue plug-in through a simple example, as well as what needs to be paid attention to. Readers who need it can follow along and learn.
Before learning, ask yourself why you want to write a vue plug-in.
In a project, especially a large project, there are many parts that need to be reused, such as loading animations and pop-up boxes. It is a little troublesome to reference one by one, and if there are too many components referenced in a vue file, the code will appear bloated, so there is a need to encapsulate the vue plug-in.
After talking about the requirements, let’s take a look at the specific implementation. At present, I have tried two different plug-in writing methods, and I will introduce them one by one.
This is my project directory. The general structure is explained like this, as simple as possible and easy to understand.
One is the loading plug-in and the other is the toast plug-in. The difference is that the loading plug-in is introduced as a component, while the toast plug-in is directly added to the mount point and called by changing the status of the method.
Currently used is Jiangzi:
toast plug-in
There are two files under the toast file, the file with the suffix vue is The skeleton of this plug-in, the first js file is to put this skeleton into the Vue global and write the operation logic.
You can take a look at the content of toast.vue:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
|
There are only two main contents here, which determine whether to display show
and what content to display message
.
After a quick look here, do you find any problems?
There is no props
attribute in this file, that is, whether it is show or message, there is no way to modify it through parent-child component communication. So how do they handle it correctly? Of. Don't worry, let's take a look at his configuration file.
index.js:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
|
The logic here can be roughly divided into the following steps:
Create an empty object, which is the name of the plug-in to be used in the future. In addition, there must be an install function in this object. Use vue's extend method to create a plug-in constructor (which can be seen as creating a subclass of vue), instantiate the subclass, and all subsequent operations can be completed through this subclass. Then add a shared method to the Vue prototype.
What needs to be mentioned here is Vue.extend()
. For example, our daily use of vue to write components looks like this:
1 2 3 |
|
This is the registration method of global components, but in fact it is syntactic sugar. The real running process is like this:
1 2 3 4 5 |
|
Vue.extend will return an object. According to most information, it can also be said to return a subclass of Vue. Since it is a subclass, there is no way to directly use the methods on the Vue prototype through it, so A new instance is required for use.
In the code console.log(instance)
the result is this:
You can see $el: p.toast
is the root node of the toast component template.
What’s confusing is that I don’t know why I need to create an empty p node and mount this instance on it. I tried commenting this code, but when I run it I get an error.
Looking for the cause of this error, it seems to be because of the
instance.$el problem in <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>document.body.appendChild(instance.$el)</pre><div class="contentsignin">Copy after login</div></div>
, then okay, Let's take a look at this on the console. WTF! ! ! ! The result turned out to be undefined
.
Then
1 |
|
Compare it with the previous picture, what do you find? Yes, $el disappeared. In other words, after I commented the sentence
1 |
|
, the mount point no longer existed. Then I tried to change this sentence:
1 |
|
$el magically came back... run. But in any case, this means that the instance must be mounted on a node for subsequent operations. The code after
is simple. It is nothing more than adding a method to change the plug-in state on the Vue prototype. Then export this object.
The next step is how to use it. Let’s take a look at how main.js is written:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
This way it can be used directly in other vue files, like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
|
By adding a method to methods to control the content written in $toast on the Vue prototype operates on the toast component.
In this way, the writing process of the toast component is over, and you can see the effect in the gif picture at the beginning.
loading plug-in
After the explanation of the previous plug-in, this part will not be so detailed. After all, most of them are no different. I will only point out the differences.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
This is just a template, passing in the data of the two parent components to control the display effect.
Let’s take a look at the configuration file of the plug-in:
1 2 3 4 5 6 7 8 9 |
|
这个和taoat的插件相比,简单了很多,依然是一个空对象,里面有一个install方法,然后在全局注册了一个组件。
比较
那介绍了这两种不同的插件编写方法,貌似没有什么不一样啊,真的是这样么?
来看一下完整的main.js和app.vue这两个文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
|
可以看出来,loading是显示的写在app.vue模板里的,而toast并没有作为一个组件写入,仅仅是通过一个方法控制显示。
来看一下html结构和vue工具给出的结构:
看出来了么,toast插件没有在挂载点里面,而是独立存在的,也就是说当执行
1 |
|
上面是我整理给大家的,希望今后会对大家有帮助。
相关文章:
The above is the detailed content of How to write vue plug-in. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

JavaScript does not require installation because it is already built into modern browsers. You just need a text editor and a browser to get started. 1) In the browser environment, run it by embedding the HTML file through tags. 2) In the Node.js environment, after downloading and installing Node.js, run the JavaScript file through the command line.
