Preface: When we use Vue, we often use and write some custom plug-ins, and then introduce them using After reading this article, you will learn:
Okay, without further ado, let’s get started! ! ! 1. Processing of install in Vuex&Vue-RouterHere are two questions for you to think about. They are like digging holes. I will answer them one by one below:
In fact, the principles of the two are the same. Here we use class Router { constructor(options) { ... } } Router.install = function(_Vue) { _Vue.mixin({ beforeCreate() { if (this.$options.router) { _Vue.prototype.$router = this.$options.router } } }) } export default Router;
In this case, let us make a bold judgment. Don’t worry, we have just solved the first problem, now let’s fill the second hole. We usually use // router/index.js import VueRouter form 'vue-router'; import Vue from 'vue'; Vue.use(VueRouter); const _router = [ ... ] const Router = new VueRouter(_router); export default Router; // main.js import Vue from 'vue'; import router from 'router'; new Vue({ router, ... }).$mount('#app'); Combining the initial example, let’s analyze it first.
bite! ! ! Elements are detected, so let's make a bold guess. 2. Internal implementation of install in Vue After reading the usage of the commonly used library export function initUse (Vue: GlobalAPI) { // Register a use method mounted on the instance Vue.use = function (plugin: Function | Object) { // Initialize the array of current plug-ins const installedPlugins = (this._installedPlugins || (this._installedPlugins = [])) // If this plugin has already been registered, then do not process it if (installedPlugins.indexOf(plugin) > -1) { return this } ... // Here comes the point! ! ! if (typeof plugin.install === 'function') { // When install is a function in the plugin, call the install method, point to the plugin, and pass a number of parameters to plugin.install.apply(plugin, args) } else if (typeof plugin === 'function') { // When the plugin itself is a function, treat it as the install method, point to the plugin, and pass a bunch of parameters into plugin.apply(null, args) } //Put the plugin into the plugin array installedPlugins.push(plugin) return this } } This part of the source code is very concise and highly readable. That is, when using, determine the plug-in type and execute Conclusion: I wonder if you have a deeper understanding of Vue's plug-in mechanism? In fact, when developing plug-ins, we can use This is the end of this article about in-depth understanding of Vue's plug-in mechanism and You may also be interested in:
|
<<: Bugs encountered when using mybatis-generator with mysql8.0.3 in IDEA
>>: In-depth explanation of MySQL stored procedures (in, out, inout)
This article example shares the specific code of ...
1. Overview I searched a lot online and found tha...
Due to business needs, there are often rush purch...
In actual projects, the up and down scroll bars a...
We are in an era of rapid development of mobile In...
Preface If we want to achieve the effect of onlin...
Frequently asked questions Access denied for user...
Table of contents 1. Enter the network card confi...
Find the problem Recently, I found a problem at w...
1. Going around in circles After going around in ...
Introduction to Angular Angular is an open source...
Volume data volume is an important concept of Doc...
Table of contents 1. Generate AST abstract syntax...
Nginx (engine x) is a high-performance HTTP and r...
Are you still looking for a way to enable Hyper-v...