简体   繁体   English

Vue-有没有办法在表单上限制$ refs的范围

[英]Vue - Is there a way to scope $refs on a form

I am passing in props to my form. 我正在传递props到表单。 So the data in the form can be populated from these props or from user entry. 因此,可以从这些props或用户输入中填充表格中的数据。 Because I'm using props I don't have data defined for these elements. 因为我使用props所以没有为这些元素定义data But instead am using v-model and passing in my props . 而是使用v-model并传递我的props

The problem then is when I want to submit, how can I get all the form data. 然后的问题是,当我要提交时,如何获取所有表单数据。

I realized I can define refs on the form inputs in order to grab the data from the form in order to submit it. 我意识到我可以在表单输入上定义refs ,以便从表单中获取数据以便提交。 But with that solution I have to step thru each data element individually. 但是,使用该解决方案,我必须单独遍历每个数据元素。 What I would like to do is just step thru the form and pull the data out in a forEach loop. 我想做的只是通过form并在forEach循环中提取数据。

Is that possible? 那可能吗?

Here is my code running. 这是我的代码正在运行。

And this is the top of my template (using Vuetify as a framework) with one of the elements of my form: 这是我模板的顶部(使用Vuetify作为框架),其中包含表单元素之一:

<template>
<v-container fluid grid-list-lg class="come_closer">
  <v-layout row wrap>
    <v-flex xs12 v-for="(creds, index) in amazonCredsArray" :key="creds.id" class="pb-4">
      <v-card class="lightpurple">
        <v-card-title>
          <v-icon class="my_dark_purple_text">language</v-icon>
          <h1 class="title oswald my_dark_purple_text pl-2 pr-5">ENTER YOUR CREDENTIALS BELOW</h1>
        </v-card-title>

     <v-form ref="form" lazy-validation>
        <v-layout xs12 row wrap class="mx-auto" >
          <v-flex xs12>
            <v-text-field
              ref="seller_id"
              :rules="[ v => sellerIdRules(v, index) ]"
              validate-on-blur
              required
              color="indigo"
              label="Amazon Seller Id"
              v-model="creds.seller_id"
              prepend-icon="person"
            ></v-text-field>
          </v-flex>

Then on submit I would have to run a method for each element that contains this: 然后在提交时,我将必须为包含此元素的每个元素运行一个方法:

this.$refs.seller_id.forEach(function(element){console.log(element.value)});

Is there an easier way to do this? 有没有更简单的方法可以做到这一点? Can I scope my refs so I can just use 1 forEach loop. 我可以限制我的refs范围,以便仅使用1 forEach循环。 Or any other suggestions? 或其他建议?

There are several ways to solve this problem. 有几种方法可以解决此问题。

The simplest is probably to create data properties that should hold the state of your input fields and initialize them with props. 最简单的方法可能是创建应包含输入字段状态的数据属性,并使用prop对其进行初始化。

props: ['value'],

data () {
   return {
      valueLocal: this.value
   }
}

Now, data properties can be used to reflect the value of your input fields again: 现在,可以使用数据属性再次反映输入字段的值:

<input v-model="valueLocal">

Downside is, if the prop value changes after the component is mounted, those changes are ignored by your component. 不利的一面是,如果在安装组件后prop值更改,则这些更改将被您的组件忽略。

You can however, extend the component definition with watchers. 但是,您可以使用观察程序扩展组件定义。 This way valueLocal is always equal to whatever changed last. 这样, valueLocal始终等于最后更改的值。 The prop or the user input. 道具或用户输入。

props: ['value'],

data () {
   return {
      valueLocal: this.value
   }
},

watch: {
    value (newValue) {
        this.valueLocal = newValue
    }
}

Note, that v-model is basically just syntax sugar for binding the value attribute and listening for the input event. 注意, v-model基本上只是语法糖,用于绑定value属性和侦听input事件。 So v-model is just short for: 因此, v-model的简称:

<input :value="valueLocal" @input="valueLocal = $event.target.value">

So you might as well keep your form component generic and let the parent component handle the state. 因此,您最好保持表单组件的通用性,并让父组件处理状态。

Form component 表单组成

The template of your form component: 表单组件的模板:

<input :value="value" @input="$emit('input', $event.target.value)">
<!-- ... --->
<input type="submit" @submit="$emit('submit')">

No data properties required, only props: 不需要数据属性,仅支持:

props: ['value']

Parent component 父组件

In the parent component template: 在父组件模板中:

<form-component :value="value" @input="value = $event" @submit="...">

Or use v-model again 或再次使用v-model

<form-component v-model="value" @submit="...">

Since you have multiple input elements in your form component this is less of an option though. 由于您的表单组件中有多个输入元素,因此这不是一个选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM