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)
Assume that a node in the three-node MGR is abnor...
To query two different tables, you need to merge ...
Table of contents Brief Analysis of MySQL Master-...
This article is based on the CentOS 7.3 system en...
1 Effect Demo address: https://www.albertyy.com/2...
Several typical values of innodb_flush_method f...
1. css: dragTable.css @charset "UTF-8";...
1. Introduction to Logrotate tool Logrotate is a ...
Preface There are often some articles on the Inte...
Table of contents K8S Advanced Features Advanced ...
Table of contents Create a vue + ts project using...
Background Controller @RequestMapping("/getP...
Preface Recently, I encountered a requirement at ...
1. Log in to the system and enter the directory: ...
Separate the front and back ends and use nginx to...