在 prop 的默认函数中访问 this

breaking

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

替代方案:

  • 把组件接收到的原始 prop 作为参数传递给默认函数;

  • 注入 API 可以在默认函数中使用。

  1. import { inject } from 'vue'
  2. export default {
  3. props: {
  4. theme: {
  5. default (props) {
  6. // `props` 是传递给组件的原始值。
  7. // 在任何类型/默认强制转换之前
  8. // 也可以使用 `inject` 来访问注入的 property
  9. return inject('theme', 'default-theme')
  10. }
  11. }
  12. }
  13. }