Source Code Analysis of Vue.use

Source Code Analysis of Vue.use

Those with vue development experience are no stranger to vue.use. When using global components such as vue-resource or vue-router, they must be introduced through the Vue.use method to take effect.

Those with Vue development experience are no stranger to vue.use. When using global components such as vue-resource or vue-router, they must be introduced through the Vue.use method to take effect. So what did vue.use do before the component was introduced?

First go to vue.use source code

Vue.use = function (plugin) {
    / * istanbul ignore if * /
    if (plugin.installed) {
      return
    }
    // additional parameters
    var args = toArray (arguments, 1);
    args.unshift (this);
    if (typeof plugin.install === 'function') {
      plugin.install.apply (plugin, args);
    } else if (typeof plugin === 'function') {
      plugin.apply (null, args);
    }
    plugin.installed = true;
    return this
  };

Suppose we introduce a plugin via Vue.use (the plugin can be temporarily understood as a variable or parameter), that is, Vue.use (plugin);

First, determine whether the attribute installed of the passed parameter plugin exists. If it exists and the logical value is true, then return directly and the code behind will not be executed. What is the purpose of this judgment? I'll talk about it later.

We first assume that the attribute installed of the plugin does not exist or is false, then continue to execute

var args = toArray (arguments, 1)

A toArray method is executed, toArray receives two parameters, arguments is the parameter set passed by the Vue.use method, such as Vue.use (a, b, c), then arguments are similar to [a, b, c] (description : Arguments are just array-like, not real arrays)

Because we only introduce a parameter plugin here, arguments are similar to [plugin].

What does toArray do? Look at the source code.

function toArray (list, start) {
  start = start || 0;
  var i = list.length-start;
  var ret = new Array (i);
  while (i--) {
    ret [i] = list [i + start];
  }
  return ret
}

When toArray (arguments, 1) is executed, a new array ret is generated with length = arguments.length-1, and then a while loop is performed, and the elements of arguments are assigned to ret in reverse order, because ret is 1 less than the arguments length.

So in the end it is equivalent to arguments assigning the remaining elements except the first element to ret. The main purpose of toArray is to convert the array-like array into a real array, so that the methods of the array can be called.

Because I only introduce one plugin parameter here, namely arguments = [plugin], toArray returns an empty array [].

Then proceed to execute, args.unshift (this), equivalent to [] .unshift (Vue), that is, args = [Vue];

Then execute

if (typeof plugin.install === 'function') {
      plugin.install.apply (plugin, args);
    } else if (typeof plugin === 'function') {
      plugin.apply (null, args);
    }

It is judged here whether the plugin's install is a function. If it is a function, immediately execute the plumn.install method. The parameters passed to the install method are the array elements in args, that is, the first parameter accepted by install is Vue.

If the plugin's install is not a function, then determine whether the plugin itself is a function. If it is a function, then execute the plugin function with the parameters as array elements in args.

Finally set plugin.installed to true. The effect of setting plugin.installed to true is to prevent the same plugin from being installed multiple times. For example, after Vue.use (plugin) is executed once, installed is true. If it is executed again, it will return to the first step.

In summary, the role of Vue.use is actually to execute a plugin function or execute the install method of plugin to register the plugin, and pass the Vue object as the first parameter to the plugin or its install method, and other parameters of use as the plugin or Install other parameters.

Give a simple example

import Vue from 'vue'

function test (a) {
   console.log (a); // Vue
}

function test1 (a, b) {
  console.log (a, b); // Vue hello
}

let oTest = {
   install: function (a, b) {
      console.log (a, b); // Vue hello1
   }
}


Vue.use (test);

Vue.use (test1, 'hello');
Vue.use (oTest, 'hello1')
console.log (oTest);
// {
  install: function () {...},
  installed: true
}

7 Best Vue Highlight Component for Your Vue App

7 Best Vue Highlight Component for Your Vue App

Vue highlight is often used to highlight text and syntax. Here are the 7 Vue highlight components I've collected.

Vue highlight is often used to highlight text and syntax. Here are the 7 Vue highlight components I've collected.

1. vue-snippets

Vue3 Snippets, This extension adds Vue3 Code Snippets into Visual Studio Code.

Download


2. vim-vue-plugin

Vim syntax and indent plugin for vue files

Download


3. vue-highlighter

Vue directive for highlight multiple istances of a word.

Download


4. vue-code-highlight

Beautiful code syntax highlighting as Vue.js component.

Download


5. Vue Prism Editor

A dead simple code editor with syntax highlighting and line numbers. 7kb/gz

Features

  • Code Editing ^^
  • Syntax highlighting
  • Undo / Redo
  • Copy / Paste
  • The spaces/tabs of the previous line is preserved when a new line is added
  • Works on mobile (thanks to contenteditable)
  • Resize to parent width and height
  • Support for line numbers
  • Support for autosizing the editor
  • Autostyling the linenumbers(optional)

Demo

Download


6. vue-highlight-words

A simple port from react-highlight-words

Vue component to highlight words within a larger body of text.

Demo

Download


7. vue-highlight-text

Vue component for highlight multiple istances of a word.

Demo

Download


Thank for read!

10 Best Vue Icon Component For Your Vue.js App

10 Best Vue Icon Component For Your Vue.js App

In this article, I will collect 10 Vue icon component to bring more interactivity, better UI design to your Vue application.

Icons are the vital element of the user interface of the product enabling successful and effective interaction with it. In this article, I will collect 10 Vue icon component to bring more interactivity, better UI design to your Vue application.

1. Animated SweetAlert Icons for Vue

A clean and simple Vue wrapper for SweetAlert's fantastic status icons. This wrapper is intended for users who are interested in just the icons. For the standard SweetAlert modal with all of its bells and whistles, you should probably use Vue-SweetAlert 2

Demo: https://vue-sweetalert-icons.netlify.com/

Download: https://github.com/JorgenVatle/vue-sweetalert-icons/archive/master.zip

2. vue-svg-transition

Create 2-state, SVG-powered animated icons.

Demo: https://codesandbox.io/s/6v20q76xwr

Download: https://github.com/kai-oswald/vue-svg-transition/archive/master.zip

3. Vue-Awesome

Awesome SVG icon component for Vue.js, with built-in Font Awesome icons.

Demo: https://justineo.github.io/vue-awesome/demo/

Download: https://github.com/Justineo/vue-awesome/archive/master.zip

4. vue-transitioning-result-icon

Transitioning Result Icon for Vue.js

A scalable result icon (SVG) that transitions the state change, that is the SVG shape change is transitioned as well as the color. Demonstration can be found here.

A transitioning (color and SVG) result icon (error or success) for Vue.

Demo: https://transitioning-result-icon.dexmo-hq.com/

Download: https://github.com/dexmo007/vue-transitioning-result-icon/archive/master.zip

5. vue-zondicons

Easily add Zondicon icons to your vue web project.

Demo: http://www.zondicons.com/icons.html

Download: https://github.com/TerryMooreII/vue-zondicons/archive/master.zip

6. vicon

Vicon is an simple iconfont componenet for vue.

iconfont
iconfont is a Vector Icon Management & Communication Platform made by Alimama MUX.

Download: https://github.com/Lt0/vicon/archive/master.zip

7. vue-svgicon

A tool to create svg icon components. (vue 2.x)

Demo: https://mmf-fe.github.io/vue-svgicon/v3/

Download: https://github.com/MMF-FE/vue-svgicon/archive/master.zip

8. vue-material-design-icons

This library is a collection of Vue single-file components to render Material Design Icons, sourced from the MaterialDesign project. It also includes some CSS that helps make the scaling of the icons a little easier.

Demo: https://gitlab.com/robcresswell/vue-material-design-icons

Download: https://gitlab.com/robcresswell/vue-material-design-icons/tree/master

9. vue-ionicons

Vue Icon Set Components from Ionic Team

Design Icons, sourced from the Ionicons project.

Demo: https://mazipan.github.io/vue-ionicons/

Download: https://github.com/mazipan/vue-ionicons/archive/master.zip

10. vue-ico

Dead easy, Google Material Icons for Vue.

This package's aim is to get icons into your Vue.js project as quick as possible, at the cost of all the bells and whistles.

Demo: https://material.io/resources/icons/?style=baseline

Download: https://github.com/paulcollett/vue-ico/archive/master.zip

I hope you like them!

Collection of 10 Vue Markdown Component for Vue.js App in 2020

Collection of 10 Vue Markdown Component for Vue.js App in 2020

Markdown is a way to style text on the web. The 10 Vue markdown components below will give you a clear view.

Markdown is a way to style text on the web. You control the display of the document; formatting words as bold or italic, adding images, and creating lists are just a few of the things we can do with Markdown.

The 10 Vue markdown components below will give you a clear view.

1. Vue Showdown

Use showdown as a Vue component.

View Demo

Download Source

2. showdown-markdown-editor

A markdown editor using codemirror and previewer using showdown for Vue.js.

View Demo

Download Source

3. markdown-it-vue

The vue lib for markdown-it.

View Demo

Download Source

4. perfect-markdown

perfect-markdown is a markdown editor based on Vue & markdown-it. The core is inspired by the implementation of mavonEditor, so perfect-markdown has almost all of the functions of mavonEditor. What's more, perfect-markdown also extends some features based on mavonEditor.

View Demo

Download Source

5. v-markdown-editor

Vue.js Markdown Editor component.

View Demo

Download Source

6. markdown-to-vue-loader

Markdown to Vue component loader for Webpack.

View Demo

Download Source

7. fo-markdown-note Component for Vue.js

fo-markdown-note is a Vue.js component that provides a simple Markdown editor that can be included in your Vue.js project.

fo-markdown-note is a thin Vue.js wrapper around the SimpleMDE Markdown editor JavaScript control.

View Demo

Download Source

8. Vue-SimpleMDE

Markdown Editor component for Vue.js. Support both vue1.0 & vue2.0

View Demo

Download Source

9. mavonEditor

A nice vue.js markdown editor. Support WYSIWYG editing mode, reading mode and so on.

View Demo

Download Source

10. vue-markdown

A Powerful and Highspeed Markdown Parser for Vue.

View Demo

Download Source

Thank for read!