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)
Copy code The code is as follows: <!DOCTYPE ht...
If you want the path following the domain name to...
This article mainly introduces the solution to th...
Scenario 1: Html: <div class="outer"...
This article shares with you the solution to the ...
What is a stored procedure Simply put, it is a se...
Preface: I reinstalled win10 and organized the fi...
Implementation principle The main graphics are co...
MySQL is a relatively easy-to-use relational data...
1. Introduction The previous program architecture...
Table of contents Overview Property settings Proc...
Table of contents Storage Engine Storage engines ...
A singly linked list can only be traversed from t...
Jenkins is an open source software project. It is...
The topic I want to share with you today is: &quo...