【问题标题】:laravel inertia how to infer typeof InertiaFormProps?laravel惯性如何推断typeof InertiaFormProps?
【发布时间】:2022-08-08 15:02:31
【问题描述】:

我有一个使用useForm 钩子的页面,这个页面是一个多步骤的表单,它们被分离到它们自己的组件中。

像这样的东西:


    export default function create(){
    
      const form = useForm({
        name: \'\',
        content: \'\',
        is_published: 0,
        some_more_fields_here
      });
    
    
    return (
    <div>
    <GeneralPageInformation form={form} />
    
    
    <SeoInformation form={form} />
    </div>
    )
}

useForm 返回的表单对象如下所示:

 InertiaFormProps<{name: string, content: string, is_published: number, rest of your fields}>

现在我尝试做这样的事情

interface IGeneralPageInformation {
  form: InertiaFormProps;
}

虽然这确实让我可以访问像 form.processtingform.recentlySuccessful 这样的东西

尝试使用 form.setData(\'all available keys should show up here)) 之类的东西时,namecontent 之类的键不可见

我可以像这样手动声明键

interface IGeneralPageInformation {
  form:  InertiaFormProps<{name: string, content: string, is_published: number, resf of the fields}>
}

但这显然不是一个非常“可扩展”的解决方案,因为每当表单发生更改时,我都必须手动编辑它。

    标签: typescript


    【解决方案1】:

    In Vue 3,

    <script setup lang="ts">
    import { InertiaForm, useForm } from "@inertiajs/inertia-vue3"
    
    const form: InertiaForm<{
                    title: string,
                    category: string,
                    types: Array<string | number>
                }> = useForm({
                    title: "",
                    category: "",
                    types: [],
                })
    </script>
    

    【讨论】:

      猜你喜欢
      • 2021-06-02
      • 2022-12-21
      • 1970-01-01
      • 2021-11-26
      • 2020-02-23
      • 2021-07-14
      • 2022-11-04
      • 2019-02-25
      • 2021-11-22
      相关资源
      最近更新 更多