渲染函数中的this。$ props导致类型错误-Vue / TypeScript

时间:2020-06-21 19:35:41

标签: javascript typescript vue.js

具有渲染功能的组件

export default Vue.extend({
  render(createElement) {
    let element = ''
    if (this.$props.link && this.$props.external) {
      element = 'a'
    } else if (this.$props.link) {
      element = 'nuxt-link'
    } else {
      element = 'button'
    }

    return createElement(
      element,
      {
        class: {
          button: true,
          link: this.$props.link === true,
          normal: this.$props.variant === 'normal',
          blue: this.$props.variant === 'blue',
          transparent: this.$props.variant === 'transparent',
        },
        attrs: {
          ...(this.$props.link &&
            this.$props.external && {
              href: this.$props.href ?? null,
            }),
          ...(this.$props.external && { target: '_blank' }),
        },
      },
      [this.$slots.icon, this.$slots.default]
    )
  },
})

上面的代码有效,但是我遇到打字错误:

Property '$props' does not exist on type 'ComponentOptions<Vue, DefaultData<Vue>, DefaultMethods<Vue>, DefaultComputed, PropsDefinition<Record<string, any>>, Record<...>>'. Did you mean 'props'?

为什么它不存在就可以使用?我注意到,如果删除if / else条件,错误就会消失。

0 个答案:

没有答案