小白教程
所有教程
关于
Search
172.70.131.129
172.70.131.129
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“Vue3 混入”的源代码
本页内容
上一节:
Vue3_路由
下一节:
Vue3_Ajax(axios)
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:Vue3 混入}}[[Category:Vue 3 教程|18]] = Vue3 混入 = 混入 (mixins)定义了一部分可复用的方法或者计算属性。混入对象可以包含任意组件选项。当组件使用混入对象时,所有混入对象的选项将被混入该组件本身的选项。 来看一个简单的实例: <sample title="" desc="" lang="html" hererun="1"> // 定义混入对象 const myMixin = { created() { this.hello() }, methods: { hello() { console.log('欢迎来到混入实例-XiaoBai.Wang!') } } } // 定义一个应用,使用混入 const app = Vue.createApp({ mixins: [myMixin] }) app.mount('#app') // => "欢迎来到混入实例-XiaoBai.Wang!" </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Vue 测试实例 - 小白教程(xiaobai.wang)</title> <script src="https://unpkg.com/vue@next"></script> </head> <body> <div id = "app"></div> <script type = "text/javascript"> // 定义混入对象 const myMixin = { created() { this.hello() }, methods: { hello() { document.write('欢迎来到混入实例-XiaoBai.Wang!') } } } // 定义一个应用,使用混入 const app = Vue.createApp({ mixins: [myMixin] }) app.mount('#app') // => "欢迎来到混入实例-XiaoBai.Wang!" </script> </body> </html> </run> == 选项合并 == 当组件和混入对象含有同名选项时,这些选项将以恰当的方式混合。 比如,数据对象在内部会进行浅合并 (一层属性深度),在和组件的数据发生冲突时以组件数据优先。 以下实例中,Vue 实例与混入对象包含了相同的方法。从输出结果可以看出两个选项合并了。 <sample title="" desc="" lang="html" hererun="1"> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Vue 测试实例 - 小白教程(xiaobai.wang)</title> <script src="https://unpkg.com/vue@next"></script> </head> <body> <div id = "app"></div> <script type = "text/javascript"> const myMixin = { data() { return { message: 'hello', foo: 'xiaobai' } } } const app = Vue.createApp({ mixins: [myMixin], data() { return { message: 'goodbye', bar: 'def' } }, created() { document.write(JSON.stringify(this.$data)) } }) </sample> 输出结果为: <sample title="" desc="" lang="html" hererun="1"> {"message":"goodbye","foo":"xiaobai","bar":"def"} </pre> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Vue 测试实例 - 小白教程(xiaobai.wang)</title> <script src="https://unpkg.com/vue@next"></script> </head> <body> <div id = "app"></div> <script type = "text/javascript"> const myMixin = { data() { return { message: 'hello', foo: 'xiaobai' } } } const app = Vue.createApp({ mixins: [myMixin], data() { return { message: 'goodbye', bar: 'def' } }, created() { document.write(JSON.stringify(this.$data)) } }) app.mount('#app') </script> </body> </html> </run> 同名钩子函数将合并为一个数组,因此都将被调用。另外,mixin 对象的钩子将在组件自身钩子之前调用。 <pre>const myMixin = { created() { console.log('mixin 对象的钩子被调用') } } const app = Vue.createApp({ mixins: [myMixin], created() { console.log('组件钩子被调用') } }) // => "mixin 对象的钩子被调用" // => "组件钩子被调用"</pre> 值为对象的选项,例如 methods、components 和 directives,将被合并为同一个对象。两个对象键名冲突时,取组件对象的键值对。 <pre> const myMixin = { methods: { foo() { console.log('foo') }, conflicting() { console.log('from mixin') } } } const app = Vue.createApp({ mixins: [myMixin], methods: { bar() { console.log('bar') }, conflicting() { console.log('from self') } } }) const vm = app.mount('#app') vm.foo() // => "foo" vm.bar() // => "bar" vm.conflicting() // => "from self" </sample> 以上实例,我们调用了以下三个方法: <pre>vm.foo(); vm.bar(); vm.conflicting();</pre> 从输出结果 methods 选项中如果碰到相同的函数名则 Vue 实例有更高的优先级会执行输出。 == 全局混入 == 也可以全局注册混入对象。注意使用! 一旦使用全局混入对象,将会影响到 所有 之后创建的 Vue 实例。使用恰当时,可以为自定义对象注入处理逻辑。 <sample title="" desc="" lang="html" hererun="1"> const app = Vue.createApp({ myOption: 'hello!' }) // 为自定义的选项 'myOption' 注入一个处理器。 app.mixin({ created() { const myOption = this.$options.myOption if (myOption) { document.write(myOption) } } }) app.mount('#app') // => "hello!" </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Vue 测试实例 - 小白教程(xiaobai.wang)</title> <script src="https://unpkg.com/vue@next"></script> </head> <body> <div id = "app"></div> <script type = "text/javascript"> const app = Vue.createApp({ myOption: 'hello!' }) // 为自定义的选项 'myOption' 注入一个处理器。 app.mixin({ created() { const myOption = this.$options.myOption if (myOption) { document.write(myOption) } } }) app.mount('#app') // => "hello!" </script> </body> </html> </run> 谨慎使用全局混入对象,因为会影响到每个单独创建的 Vue 实例 (包括第三方模板)。
返回至“
Vue3 混入
”。
上一节:
Vue3_路由
下一节:
Vue3_Ajax(axios)