Vue 3.0 在prop的默認(rèn)函數(shù)中訪問(wèn)this

2021-07-16 11:25 更新

生成 prop 默認(rèn)值的工廠函數(shù)不再能訪問(wèn) this

替代方案:

  • 把組件接收到的原始 prop 作為參數(shù)傳遞給默認(rèn)函數(shù);
  • 注入 API 可以在默認(rèn)函數(shù)中使用。

import { inject } from 'vue'


export default {
  props: {
    theme: {
      default (props) {
        // `props` 是傳遞給組件的原始值。
        // 在任何類型/默認(rèn)強(qiáng)制轉(zhuǎn)換之前
        // 也可以使用 `inject` 來(lái)訪問(wèn)注入的 property
        return inject('theme', 'default-theme')
      }
    }
  }
}
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)