Summary about using mixins

Mixin Overview

Mixins provide a very flexible way to distribute reusable functionality in Vue components. A mixin can contain arbitrary component options. When a component uses a mixin, all of the mixin's options will be "mixed" into the options of the component itself.

MixinsFeatures

Methods and parameters are not shared among components
Options whose values ​​are objects, such as methods, components, etc., will be merged, and components with key conflicts will overwrite the mixed objects. /span>
Options whose value is a function, such as created, mounted, etc., will be merged and called. The hook function in the mixed object is called before the hook function in the component

The difference with vuex

vuex: used for state management. The variables defined in it can be used and modified in each component. After the value of this variable is modified in any component, the value of this variable in other components will also follow. Modification
Mixins: Shared variables can be defined and used in each component. After being introduced into the component, each variable is independent of each other, and the modification of the value will not affect each other in the component a>

Differences from public components

Component: Introducing a component into the parent component is equivalent to giving an independent space in the parent component for the child component to use, and then passing the value based on props, but essentially the two are relatively independent
Mixins: After the component is introduced, it is merged with the objects and methods in the component, which is equivalent to extending the objects and methods of the parent component, and can be understood as forming a new component

Guess you like

Origin blog.csdn.net/Admin_yws/article/details/124452740