2.18. Vue3 blending

发布时间 :2025-10-25 12:21:08 UTC      

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:

2.18.1. 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.

2.18.2. 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 methods components 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.

2.18.3. 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.

2.18.4. 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.

Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.