Vue3 blending


Release date:2024-03-08 Update date:2024-03-08 Editor:admin View counts:48

Label:

Vue3 blending

Mixins defines some of the reusable methods or computational properties. Blending objects can contain any component option. When a component uses a blend object, all options for blending the object are mixed into the component’s own options.

Let’s look at a simple example:

Example

//Define blending objects constmyMixin={created(){this.hello()},methods:{hello(){console.log
('Welcome to the mixed in instance-RUNOOB!')}}}//Define an application that uses mixing constapp=Vue.createApp({mixins:[myMixin]})app.mount('#app')//=>
"Welcome to the mixed in instance-RUNOOB!"

Option merge

When components and blended objects have options of the same name, these options are mixed in an appropriate manner.

For example, data objects are shallowly merged internally (a layer of attribute depth), giving priority to component data when there is a conflictwith component data.

In the following example, the Vue instance contains the same methods as the blending object. From the output, you can see that the two options have beenmerged.

Example

<!DOCTYPEhtml> <html> <head> <metacharset="utf-8"> <title>Vue Test Example -
Rookie Tutorial(runoob.com)</title>
<scriptsrc="https://unpkg.com/vue@next"></script> </head> <body>
<divid="app"></div>
<scripttype="text/javascript">constmyMixin={data(){return{message:'hello',foo:'runoob'}}}constapp=
Vue.createApp({mixins:[myMixin],data(){return{message:'goodbye',bar:'def'}},created(){document.write(JSON.stringify(this.$data))}})

The output is as follows:

{"message":"goodbye","foo":"runoob","bar":"def"}

Hook functions with the same name will be merged into one array, so they will all be called. In addition, mixin object’s hook will be called before the component’s own hook.

const myMixin = {
  created() {
    console.log('mixin The hook of the object is called')
  }
}

const app = Vue.createApp({
  mixins: [myMixin],
  created() {
    console.log('Component hook called')
  }
})

// => "mixin The hook of the object is called"
// => "The hook of the object is called"

An option whose value is an object, such as methodscomponents and directives will be merged into the same object When the key names of two objects conflict, take the key-value pair of the component object.

Example

constmyMixin={methods:{foo(){console.log('foo')},conflicting(){console.log('from
mixin')}}}constapp=Vue.createApp({mixins:[myMixin],methods:{bar(){console.log('bar')},conflicting(){console.log('from
self')}}})constvm=app.mount('#app')vm.foo()//=> "foo"vm.bar()//=>
"bar"vm.conflicting()//=> "from self"

For the above example, we call the following three methods:

vm.foo();
vm.bar();
vm.conflicting();

Output results from methods option, the Vue instance has a higher priority to execute the output if it encounters the same function name.

Global blending

You can also register blending objects globally. Pay attention to the use! Once the global blending object is used, all later created Vue instances will be affected. When used appropriately, you can inject processing logic into custom objects.

Example

constapp=Vue.createApp({myOption:'hello!'})//Customized options 'myOption'
Inject a processor.app.mixin({created(){constmyOption=this.$options.myOptionif(myOption){document.write(myOption)}}})app.mount('#app')//=>
"hello!"

Use global blending objects with caution, as each individually created Vue instance (including third-party templates) is affected.

Powered by TorCMS (https://github.com/bukun/TorCMS).