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

2021-07-16 11:25 更新

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

替代方案:

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

  1. import { inject } from 'vue'
  2. export default {
  3. props: {
  4. theme: {
  5. default (props) {
  6. // `props` 是傳遞給組件的原始值。
  7. // 在任何類型/默認強制轉(zhuǎn)換之前
  8. // 也可以使用 `inject` 來訪問注入的 property
  9. return inject('theme', 'default-theme')
  10. }
  11. }
  12. }
  13. }
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號