Vue 3.0 混入

2021-07-16 11:41 更新

#基礎(chǔ)

混入 (mixin) 提供了一種非常靈活的方式,來分發(fā) Vue 組件中的可復(fù)用功能。一個混入對象可以包含任意組件選項。當(dāng)組件使用混入對象時,所有混入對象的選項將被“混合”進入該組件本身的選項。

例子:

  1. // define a mixin object
  2. const myMixin = {
  3. created() {
  4. this.hello()
  5. },
  6. methods: {
  7. hello() {
  8. console.log('hello from mixin!')
  9. }
  10. }
  11. }
  12. // define an app that uses this mixin
  13. const app = Vue.createApp({
  14. mixins: [myMixin]
  15. })
  16. app.mount('#mixins-basic') // => "hello from mixin!"

#選項合并

當(dāng)組件和混入對象含有同名選項時,這些選項將以恰當(dāng)?shù)姆绞竭M行“合并”。

比如,數(shù)據(jù)對象在內(nèi)部會進行遞歸合并,并在發(fā)生沖突時以組件數(shù)據(jù)優(yōu)先。

  1. const myMixin = {
  2. data() {
  3. return {
  4. message: 'hello',
  5. foo: 'abc'
  6. }
  7. }
  8. }
  9. const app = Vue.createApp({
  10. mixins: [myMixin],
  11. data() {
  12. return {
  13. message: 'goodbye',
  14. bar: 'def'
  15. }
  16. },
  17. created() {
  18. console.log(this.$data) // => { message: "goodbye", foo: "abc", bar: "def" }
  19. }
  20. })

同名鉤子函數(shù)將合并為一個數(shù)組,因此都將被調(diào)用。另外,混入對象的鉤子將在組件自身鉤子之前調(diào)用。

  1. const myMixin = {
  2. created() {
  3. console.log('mixin hook called')
  4. }
  5. }
  6. const app = Vue.createApp({
  7. mixins: [myMixin],
  8. created() {
  9. console.log('component hook called')
  10. }
  11. })
  12. // => "混入對象的鉤子被調(diào)用"
  13. // => "組件鉤子被調(diào)用"

值為對象的選項,例如 methodscomponentsdirectives,將被合并為同一個對象。兩個對象鍵名沖突時,取組件對象的鍵值對。

  1. const myMixin = {
  2. methods: {
  3. foo() {
  4. console.log('foo')
  5. },
  6. conflicting() {
  7. console.log('from mixin')
  8. }
  9. }
  10. }
  11. const app = Vue.createApp({
  12. mixins: [myMixin],
  13. methods: {
  14. bar() {
  15. console.log('bar')
  16. },
  17. conflicting() {
  18. console.log('from self')
  19. }
  20. }
  21. })
  22. const vm = app.mount('#mixins-basic')
  23. vm.foo() // => "foo"
  24. vm.bar() // => "bar"
  25. vm.conflicting() // => "from self"

#全局混入

你還可以為 Vue 應(yīng)用程序全局應(yīng)用 mixin:

  1. const app = Vue.createApp({
  2. myOption: 'hello!'
  3. })
  4. // 為自定義的選項 'myOption' 注入一個處理器。
  5. app.mixin({
  6. created() {
  7. const myOption = this.$options.myOption
  8. if (myOption) {
  9. console.log(myOption)
  10. }
  11. }
  12. })
  13. app.mount('#mixins-global') // => "hello!"

混入也可以進行全局注冊。使用時格外小心!一旦使用全局混入,它將影響每一個之后創(chuàng)建的組件 (例如,每個子組件)。

  1. const app = Vue.createApp({
  2. myOption: 'hello!'
  3. })
  4. // 為自定義的選項 'myOption' 注入一個處理器。
  5. app.mixin({
  6. created() {
  7. const myOption = this.$options.myOption
  8. if (myOption) {
  9. console.log(myOption)
  10. }
  11. }
  12. })
  13. // 將myOption也添加到子組件
  14. app.component('test-component', {
  15. myOption: 'hello from component!'
  16. })
  17. app.mount('#mixins-global')
  18. // => "hello!"
  19. // => "hello from component!"

大多數(shù)情況下,只應(yīng)當(dāng)應(yīng)用于自定義選項,就像上面示例一樣。推薦將其作為插件發(fā)布,以避免重復(fù)應(yīng)用混入。

#自定義選項合并策略

自定義選項將使用默認(rèn)策略,即簡單地覆蓋已有值。如果想讓自定義選項以自定義邏輯合并,可以向 app.config.optionMergeStrategies 添加一個函數(shù):

  1. const app = Vue.createApp({})
  2. app.config.optionMergeStrategies.customOption = (toVal, fromVal) => {
  3. // return mergedVal
  4. }

合并策略接收在父實例和子實例上定義的該選項的值,分別作為第一個和第二個參數(shù)。讓我們來檢查一下使用 mixin 時,這些參數(shù)有哪些:

  1. const app = Vue.createApp({
  2. custom: 'hello!'
  3. })
  4. app.config.optionMergeStrategies.custom = (toVal, fromVal) => {
  5. console.log(fromVal, toVal)
  6. // => "goodbye!", undefined
  7. // => "hello", "goodbye!"
  8. return fromVal || toVal
  9. }
  10. app.mixin({
  11. custom: 'goodbye!',
  12. created() {
  13. console.log(this.$options.custom) // => "hello!"
  14. }
  15. })

如你所見,在控制臺中,我們先從 mixin 打印 toValfromVal,然后從 app 打印。如果存在,我們總是返回 fromVal,這就是為什么 this.$options.custom 設(shè)置為 你好! 最后。讓我們嘗試將策略更改為始終從子實例返回值:

  1. const app = Vue.createApp({
  2. custom: 'hello!'
  3. })
  4. app.config.optionMergeStrategies.custom = (toVal, fromVal) => toVal || fromVal
  5. app.mixin({
  6. custom: 'goodbye!',
  7. created() {
  8. console.log(this.$options.custom) // => "goodbye!"
  9. }
  10. })

在 Vue 2 中,mixin 是將部分組件邏輯抽象成可重用塊的主要工具。但是,他們有幾個問題:

  • mixin 很容易發(fā)生沖突:因為每個特性的屬性都被合并到同一個組件中,所以為了避免 property 名沖突和調(diào)試,你仍然需要了解其他每個特性。
  • 可重用性是有限的:我們不能向 mixin 傳遞任何參數(shù)來改變它的邏輯,這降低了它們在抽象邏輯方面的靈活性

為了解決這些問題,我們添加了一種通過邏輯關(guān)注點組織代碼的新方法:組合式 API

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號