【问题标题】:How do you infer types of props in the component of Vue 3?如何推断 Vue 3 组件中的 props 类型?
【发布时间】:2020-11-11 23:23:58
【问题描述】:

vue3的组件中如何获取props的值 对于下面的组件,我想通过defineComponent的泛型参数传递来推断props和data的类型,但是这样做无法获取props的值。如何修改?

import { defineComponent , Prop, Ref, toRefs} from 'vue'

interface MProps {
    day:  number
}

interface MData {
    day: Ref<number>,
}

export default defineComponent<MProps, MData>({

    setup(props, content) {
        const { day } = toRefs(props)
        console.log('props.day', props.day)

        return  {
            day: day,
        }
    },

    render() {
        return <div>
            {this.day}
        </div>
    }
})

【问题讨论】:

    标签: typescript vuejs3


    【解决方案1】:

    您正在使用defineComponent 中的the first signature,它需要一个直接的setup 函数。但问题是,extendComponentOptionsBase 不让你添加一个render 函数(这样的实现只在其他重载/签名中可用)。

    但是,如果您愿意,您可以将 JSX 元素作为RenderFunction 返回,并带有第一个签名;虽然很遗憾,正如下面的 cmets 所讨论的那样——这并没有按预期工作(因为父/消费组件传递的 props 实际上是作为 DOM 属性传递的!)。希望我们不会在这里遗漏一些非常明显的东西,比如必须在某个地方实际定义道具?

    export default defineComponent<MProps, MData>(function setup(props) {
      const { day } = toRefs(props);
    
      return () => (
        <div>{day}</div>
      );
    })
    

    但是,如果您可以使用第二个签名,则以下方法(使用 Props Annotation)将为您提供相同的类型推断效果,但会牺牲您的结构化合同 (interface)。

    import { defineComponent, PropType, toRefs } from 'vue';
    
    type MonthNames = 'January' | 'February' | 'March';
    
    interface ITodo {
      done: boolean;
      notes: string;
      dateCreated: Date;
      dateModified: Date;
    }
    
    export default defineComponent({
      props: {
        // We won't have issue with primitive values as they can be automatically inferred
        day: Number,
    
        // Here's our challenge. And `PropType` to the rescue!
        monthNames: Array as PropType<MonthNames>,
    
        todo: Object as PropType<ITodo>,
    
        todoList: Array as PropType<ITodo[]>,
    
        // Another variation: With custom class 
        todoList2: class TodoList implements Array<ITodo> {
    
        }
      },
    
      setup(props) {
        const { day, monthNames, todo, todoList } = toRefs(props);
    
        // If you use VSCode, you should get intellisense with the list of month names 
        // as soon as you type an opening quote
        monthNames.value = 'January';
    
        todo.value.done = true;
    
        todoList.value.push({
          done: false
        });
    
        return {}
      }
    });
    

    【讨论】:

    • 请注意不要直接修改 props,上面只是一个示例,展示了类型推断如何与 props 注释一起使用。
    • 哈哈哈我有点明白了,4中的签名对应不同的输入。其实我要的是第四个签名ComponentOptionsWithObjectProps.Generics 给了props的约束,传入defineComponent函数的对象也有props属性。如何正确使用它。
    • 对不起,我不确定你的意思,你能详细说明一下吗?但是,是的,我认为我上面的示例实际上使用了第四个签名。
    • 啊,我想我明白了。所以,我尝试了不同的重载,最后使用了第一个,这似乎更接近你最初的 props 约束尝试。您需要做的就是将setup 函数作为defineComponent&lt;MProps, MData&gt; 的第一个参数传递。但是,我遇到了一个问题,即当父/消费组件分配值时,它没有绑定组件上的任何道具。我想知道这是否是一个错误。
    • 太棒了!这是你所理解的。当我尝试使用`defineComponent`。这种方式来约束道具的类型,我应该能够将它们包含在函数参数中。但是如果我不定义它,当父组件传递属性给它时,它会作为DOM属性而不是props属性传递给Com组件实例。
    猜你喜欢
    • 2022-10-14
    • 2023-03-18
    • 1970-01-01
    • 2021-01-15
    • 2021-08-24
    • 2021-03-03
    • 2020-09-18
    • 1970-01-01
    • 2020-05-14
    相关资源
    最近更新 更多