【问题标题】:Advice on how to go about splitting a form into multiple "pages"/"tabs" in Vue?关于如何在 Vue 中将表单拆分为多个“页面”/“选项卡”的建议?
【发布时间】:2020-09-13 10:56:30
【问题描述】:

我是 Vue 开发的新手,我希望针对我正在尝试解决的问题获得一些建议或更好的解决方案。

目前我正在尝试构建一个向导工具,它可以采用一种跨越多个页面的形式(在向导内)。我希望它的行为方式类似于:

  <myWizard>
    <form>
      <myPage>
        <label />
        <input/>
      </myPage>
      <myPage>
        <label />
        <input/>
      </myPage>
    </form>
  </myWizard>

我的主要问题基本上是因为它是一个我不知道/知道&lt;myWizard&gt; 标签之间的工具,因为它取决于使用该工具的人。

我已经有了一个基本设置,可以使用 BootStrap 将内容呈现到单独的选项卡(页面)中,但想知道将表单内容分离到不同页面的最佳方法是什么。

例如,是否可以使用&lt;slot&gt; 接收&lt;myWizard&gt; 标记之间的所有内容,然后让 myPage 成为另一个组件,允许我在向导中将它们之间的内容呈现到单独的页面中?

虽然这样做并尝试识别插槽内的“标签”/“组件”似乎非常迂回,我还没有专门研究过。

感谢您提供的任何建议。

【问题讨论】:

    标签: javascript typescript vue.js frontend bootstrap-vue


    【解决方案1】:

    你可以这样做:

    // router.js
    const vueRoutes = [
      {
        path: '/',
        name: 'home',
        component: Home,
      },
    (...)
      {
        path: '/wizard',  // Define main route for the form
        name: 'wizard',
        component: AppWizard,
        children: [
          {
            path: 'page1',
            name: 'page1',
            component: FormPart1, // If the route is '/wizard/page1' it will render the FormPart1 component
          },
          {
            path: 'page2',
            name: 'page2',
            component: FormPart2, // If the route is '/wizard/page2' it will render the FormPart2 component
          }
        ],
      },
    (...)
    ]
    
    // AppWizard.vue
    <template>
      <my-wizard>
        <form>
          <router-view>
        </form>
      </my-wizard>
    </template>
    
    // FormPart1.vue
    <template>
      <fieldset>
        <label1/>
        <input1>
        <label2/>
        <input2>
      </fieldset>
    </template>
    
    
    // FormPart2.vue
    <template>
      <fieldset>
        <label3/>
        <input3>
        <label4/>
        <input4>
      </fieldset>
    </template>
    

    当然,您可以随意将wizard/page1 替换为pages/1 或其他任何东西。

    【讨论】:

    • 感谢您的反馈,虽然我可能有点困惑,这是否意味着设置表单它会非常静态,因为向导中保存的表单始终是我自己构建的表单?刚刚意识到我没有提到它,但我希望让向导成为创建向导的工具。所以诸如页数和表单的实际内容之类的东西我不知道。
    • 嗯,这是一个完全不同的故事:-D
    • 你可以试试动态路由router.vuejs.org/guide/essentials/…加上ESM动态import()
    猜你喜欢
    • 2013-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多