Vue 2 Internal Features from the Ground Up

Introducing Writing Plugins

Evan You

Evan You

Creator of Vue.js
Vue 2 Internal Features from the Ground Up

Check out a free preview of the full Vue 2 Internal Features from the Ground Up course

The "Introducing Writing Plugins" Lesson is part of the full, Vue 2 Internal Features from the Ground Up course featured in this preview video. Here's what you'd learn in this lesson:

After reviewing a high-level understanding, Evan examines the makeup of a Vue plugin.


Transcript from the "Introducing Writing Plugins" Lesson

>> Evan You: So we're done with the reactivity system. We got something very basic working. And if inside the functions, you're doing some manipulations, you've essentially built a tiny reactivity engine that can automatically keep your state and in sync, right? So that's the very simple version of how vue's update system actually works.

And next we are going to take little detour into another topic. Let's talk about writing plugins. So, you probably used a plugin before with vue.use. And how many of you have read the page about writing plugins in the docs? No one?
>> Speaker 2: Glanced?
>> Evan You: Glanced, okay. All right, so essentially a plugin is just a function that receives Vue as the first argument and optionally second argument options.

And then you do something with Vue inside your plugin. That's the higher level Vue of it, but some of the commonly used techniques when you're writing plugins involves this Vue.mixin API. How many of you have used mixins?
>> Evan You: Okay. So mixins are essentially parts reusable snippets or parts of Vue component options that can be mixed into an existing component, all right?

So you can use them on a per component basis but Vue.mixin is a global API that will apply a mixin to every instance created from then on, right? So it's very powerful, but the greater the power the greater the responsibility. So you don't do crazy things inside the global mixins, all right, because it affects every instance.

But it's very commonly, if you want to apply some global functionality to your Vue app, you will be using global mixins. And usually it's a better idea to wrap this with a plugin interface because plugins are automatically deduped. If you have multiple vue.use calls to the same plugin it will prevent it from being applied over and over again.

The vue.mixin doesn't do that. So it's best practice to always wrap global mixins in a plugin interface. And it's also more consistent when you see your entry script. You always see vue.use, this plugin, that plugin. It's more consistent that way. And the other commonly used thing is this dollar options property, instance property.

So probably not many of you know this thing exists, but this dot dollar options exist on every component and this contains the merged, normalized options object of that specific instance, because we know Vue component options can come from a lot of places. It can come from global mixins.

It can come from the component's own definition. It can come from the options you pass to it when you're creating the instance, all right. And it can be also from an answers component if you have further chains of view.extend along the way. So all of these options need to be merged and normalized into this final options object for that instance, and that is $options for each instance.

And interestingly, this also contains any custom options that you added to that component. So it doesn't have to be a built-in option. So we will just merge everything in here.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now