This time I will bring you a detailed explanation of the steps for using Mixins in Vue.js. What are theprecautionsfor using Mixins in Vue.js. Here are practical cases, let’s take a look.
A very common scenario: There are two very similar components. They have very similar basic functions, but there are enough differences between them. How to choose? Should we combine them? Should it be divided into two completely different components? Or create a basic component and then define enough props to easily distinguish usage scenarios?
Neither approach is perfect: if you separate them into two completely different components, you may increase the risk of needing to modify both components at the same time when requirements change (functional changes). This violates the "DRY" premise. On the other hand, too many props can quickly become cluttered, and, forcing maintainers, or even yourself, to Having to understand the context of these props before you can use them can be very disappointing.
Vue's Mixins are a very practical way ofprogramming, because the ultimate practical programming is to make the code easier to understand by continuously reducing moving parts. (Michael Feathers has a goodquoteon this). A mixin allows you to encapsulate a functionality, so that you can use it in different components throughout your application. If the mixin is created correctly, They are pure – they do not modify or change content outside the scope of thefunction, so you can execute them in multiple places and as long as the input values are the same, You always get the same results very reliably. This is really powerful.
Meet Mixins
Mixins are a very flexible way to distribute reusable functionality in Vue components. MixinObjectscan contain arbitrary component options. When using a mixin object as a component, all the mixin object's options will be mixed into the options of the component itself.
chestnut
Let's say we have a few different components whose job it is to toggle state booleans, a modal and a tooltip. These tooltips don't have much in common with modals, except for this functionality: they look different, they use differently, but their logic is similar.
//modal const Modal = { template: '#modal', data() { return { isShowing: false } }, methods: { toggleShow() { this.isShowing = !this.isShowing; } } } //tooltip const Tooltip = { template: '#tooltip', data() { return { isShowing: false } }, methods: { toggleShow() { this.isShowing = !this.isShowing; } } }
We can extract logic from it and create reusable parts:
const toggle = { data() { return { isShowing: false } }, methods: { toggleShow() { this.isShowing = !this.isShowing; } } } const Modal = { template: '#modal', mixins: [toggle] }; const Tooltip = { template: '#tooltip', mixins: [toggle] };
duang — A small and simple :chestnut: let us know that Mixins are so interesting, convenient and practical for encapsulating some reusable functions.
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
The above is the detailed content of Detailed explanation of the steps for using Mixins in Vue.js. For more information, please follow other related articles on the PHP Chinese website!