【问题标题】:Remove error when passing prop to JSX component in Vue.js在 Vue.js 中将 prop 传递给 JSX 组件时删除错误
【发布时间】:2021-09-25 11:53:04
【问题描述】:

我有一个boiler plate vue-cli install with TypeScript working to render JSX。但是,在将属性传递给 HelloWorld 组件时,如下所示:

export default Vue.extend({
  name: 'App',
  functional: true,
  render() {
    return (
      <div id="app">
        <HelloWorld msg="Welcome to Your Vue.js + TypeScript App" />
      </div>
    );
  },
});

通过为功能组件文件添加context,我“成功”通过了msg,如下所示:

export default Vue.extend({
  name: 'HelloWorld',
  functional: true,
  props: {
    msg: String,
  },
  render(h, context) {
    return (
      <div class="hello">
        <h1>{ context.props.msg }</h1>
      </div>
    );
  },
});

但是,我的终端仍然出现这个可怕的错误:

ERROR in /home/scott/convrrt-component/view-play/src/App.vue(25,21):
25:21 No overload matches this call.
  Overload 1 of 3, '(options?: ThisTypedComponentOptionsWithArrayProps<{ msg: string; } & Vue, object, object, object, never> | undefined): CombinedVueInstance<{ ...; } & Vue, object, object, object, Record<...>>', gave the following error.
    Type '{ msg: string; }' is not assignable to type 'ThisTypedComponentOptionsWithArrayProps<{ msg: string; } & Vue, object, object, object, never>'.
      Property 'msg' does not exist on type 'ThisTypedComponentOptionsWithArrayProps<{ msg: string; } & Vue, object, object, object, never>'.
  Overload 2 of 3, '(options?: ThisTypedComponentOptionsWithRecordProps<{ msg: string; } & Vue, object, object, object, object> | undefined): CombinedVueInstance<...>', gave the following error.
    Type '{ msg: string; }' is not assignable to type 'ThisTypedComponentOptionsWithRecordProps<{ msg: string; } & Vue, object, object, object, object>'.
      Property 'msg' does not exist on type 'ThisTypedComponentOptionsWithRecordProps<{ msg: string; } & Vue, object, object, object, object>'.
  Overload 3 of 3, '(options?: ComponentOptions<{ msg: string; } & Vue, DefaultData<{ msg: string; } & Vue>, DefaultMethods<{ msg: string; } & Vue>, DefaultComputed, PropsDefinition<...>, Record<...>> | undefined): CombinedVueInstance<...>', gave the following error.
    Type '{ msg: string; }' is not assignable to type 'ComponentOptions<{ msg: string; } & Vue, DefaultData<{ msg: string; } & Vue>, DefaultMethods<{ msg: string; } & Vue>, DefaultComputed, PropsDefinition<...>, Record<...>>'.
      Property 'msg' does not exist on type 'ComponentOptions<{ msg: string; } & Vue, DefaultData<{ msg: string; } & Vue>, DefaultMethods<{ msg: string; } & Vue>, DefaultComputed, PropsDefinition<...>, Record<...>>'.
    23 |         // Needed to add in .eslintrc in rules a property of:  "global-require": 0
    24 |         <img alt="Vue logo" src={require('@/assets/logo.png')} />
  > 25 |         <HelloWorld msg="Welcome to Your Vue.js + TypeScript App" />
       |                     ^
    26 |       </div>
    27 |     );
    28 |   },
Version: typescript 4.1.6

所以它“工作”令人困惑(它将字符串传递并呈现给 html),但随后给了我这个错误,似乎表明它不应该工作,因为类型不匹配和/或缺少 'msg ' 属性,所有这些都不是真的,基于HelloWorld 组件中的props 定义。

问题:发生了什么,如何消除终端中显示的这个错误?

【问题讨论】:

    标签: typescript vue.js properties jsx


    【解决方案1】:

    编辑: 似乎这是 Vue2 的一个已知问题,如果您尝试使用 Vue2,您基本上可以忘记 tsx 文件。至少当前的插件等似乎没有奏效。 不过在 Vue3 中运行良好,仅供参考。

    根据Vue的types文件

      render?(this: undefined, createElement: CreateElement, context: RenderContext<Props>): VNode | VNode[];
    
    

    所以我猜测在渲染函数中使用 this 不是一个好主意,应该使用上下文。

    我相信你应该为你的 msg 属性提供一个类型和可能的默认值

    import Vue from 'vue';
    
    export default Vue.extend({
      name: 'HelloWorld',
      functional: true,
      props: {
        msg: {
          type: String,
          default: ''
        }
      },
      render(h, context) {
        return (
          <div class="hello">
            <h1>{context.props.msg}</h1>
          </div>
        );
      }
    });
    

    使用 TypeScript 和 Vue2 JSX 不会为我抛出任何错误。

    【讨论】:

    • 谢谢,但这并没有没有为我解决。我已经在使用context,并尝试添加default 以查看是否解决了问题,但我仍然遇到与原始帖子中所述相同的错误。
    • 你在使用github.com/vuejs/jsx吗?根据自述文件,您应该可以使用const HelloWorld = ({ props }) =&gt; &lt;p&gt;hello {props.message}&lt;/p&gt; 作为功能组件。是否也会触发上述错误?
    • 是的,这就是我正在使用的包。我得到了与上面三个重载消息相同类型的错误的不太详细的形式:Type '{ msg: string; }' is not assignable to type '{ props: any; }'. Property 'msg' does not exist on type '{ props: any; }'.
    • 试试const HelloWorld = ({ props }: { msg: string }) =&gt; &lt;p&gt;hello {props.message}&lt;/p&gt;
    • 出现稍微不同的错误(仍然呈现页面):Property 'props' does not exist on type '{ msg: string; }'.
    猜你喜欢
    • 2018-02-18
    • 2017-12-30
    • 2020-05-22
    • 2019-07-25
    • 1970-01-01
    • 2020-11-15
    • 2022-11-13
    • 2021-05-12
    • 2023-03-04
    相关资源
    最近更新 更多