codecamp

Vue 3.0 在prop的默认函数中访问this

生成 prop 默认值的工厂函数不再能访问 this

替代方案:

  • 把组件接收到的原始 prop 作为参数传递给默认函数;
  • 注入 API 可以在默认函数中使用。

import { inject } from 'vue'


export default {
  props: {
    theme: {
      default (props) {
        // `props` 是传递给组件的原始值。
        // 在任何类型/默认强制转换之前
        // 也可以使用 `inject` 来访问注入的 property
        return inject('theme', 'default-theme')
      }
    }
  }
}
Vue 3.0 按键修饰符
Vue 3.0 渲染函数API
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定