Vue2如何只提交表单值?

发布于 2025-01-11 23:41:00 字数 2079 浏览 0 评论 0原文

我有一个表单页面,我用它来创建和更新

我的表单字段是这样的;

在此处输入图片描述

content: (...)
i18n: (...)
image: (...)
name: (...)
orderIndex: (...)
position: (...)

我可以成功提交请求。

当我们进行更新过程时,我通过这种方式获取数据并同步它。我收到了额外的数据 (this.myForm = response.data)

当我发送更新请求时,我只想发送表单字段,但它就像这样

我不想发送 createdAt、deleted、updatedAt、_id 字段

在此处输入图像描述

content: (...)
createdAt: (...)
deleted: (...)
i18n: (...)
image: (...)
isEnabled: (...)
name: (...)
orderIndex: (...)
position: (...)
updatedAt: (...)
_id: (...)

如何提交仅表单字段? (我正在使用 element-ui 顺便说一句)

是否有类似 this.$refs.myForm.fieldsthis.$refs.myForm.values 我找不到它

例如,Angular 反应形式有这样的东西 --> this.testimonialForm.patchValue(response.data);

  data() {
    return {
      id: null,
      testimonialForm: {
        name: '',
        position: '',
        content: '',
        orderIndex: '',
        i18n: '',
        image: {
          path: ''
        }
      }
    }
  },
  computed: {
    ...mapState({
      testimonialData: state => state.testimonial.testimonial
    })
  },
  created() {
    if (this.$route.params.id) {
      this.id = this.$route.params.id
      this.fnGetTestimonialInfo(this.id)
    }
  },
  methods: {
    fnCreateTestimonial() {
      this.$store.dispatch('testimonial/create', this.testimonialForm).then(() => {
        this.$router.push('/testimonial/list')
      })
    },
    fnUpdateTestimonial() {
      const data = { id: this.id, data: this.testimonialForm }
      this.$store.dispatch('testimonial/update', data).then(() => {
        this.$router.push('/testimonial/list')
      })
    },
    fnGetTestimonialInfo(id) {
      this.$store.dispatch('testimonial/get', id).then(() => {
        this.testimonialForm = this.testimonialData
      })
    },
  }

I have a form page and I use it for both create and update

My form fields are like this;

enter image description here

content: (...)
i18n: (...)
image: (...)
name: (...)
orderIndex: (...)
position: (...)

I can successfully submit a request.

When we come to the update process, I get the data in this way and sync it. I'm getting extra data (this.myForm = response.data)

When I send an update request I just want the form fields to go but it goes like this

I don't want to send createdAt, deleted, updatedAt, _id fields

enter image description here

content: (...)
createdAt: (...)
deleted: (...)
i18n: (...)
image: (...)
isEnabled: (...)
name: (...)
orderIndex: (...)
position: (...)
updatedAt: (...)
_id: (...)

How can I submit only form fields? (I am using element-ui btw)

Is there something like this.$refs.myForm.fields or this.$refs.myForm.values I couldn't find it

For example Angular reactive form has something like this --> this.testimonialForm.patchValue(response.data);

  data() {
    return {
      id: null,
      testimonialForm: {
        name: '',
        position: '',
        content: '',
        orderIndex: '',
        i18n: '',
        image: {
          path: ''
        }
      }
    }
  },
  computed: {
    ...mapState({
      testimonialData: state => state.testimonial.testimonial
    })
  },
  created() {
    if (this.$route.params.id) {
      this.id = this.$route.params.id
      this.fnGetTestimonialInfo(this.id)
    }
  },
  methods: {
    fnCreateTestimonial() {
      this.$store.dispatch('testimonial/create', this.testimonialForm).then(() => {
        this.$router.push('/testimonial/list')
      })
    },
    fnUpdateTestimonial() {
      const data = { id: this.id, data: this.testimonialForm }
      this.$store.dispatch('testimonial/update', data).then(() => {
        this.$router.push('/testimonial/list')
      })
    },
    fnGetTestimonialInfo(id) {
      this.$store.dispatch('testimonial/get', id).then(() => {
        this.testimonialForm = this.testimonialData
      })
    },
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

じее 2025-01-18 23:41:00

像这样解决

const pick = require('lodash/pick')
const formKeys = Object.keys(this.testimonialForm)
this.testimonialForm = pick(this.testimonialData, formKeys)

感谢@gguney 的指导。

Solved like this :

const pick = require('lodash/pick')
const formKeys = Object.keys(this.testimonialForm)
this.testimonialForm = pick(this.testimonialData, formKeys)

Thanks to @gguney for the guidance.

灰色世界里的红玫瑰 2025-01-18 23:41:00

首先,您必须从后端获取对象。您不需要去您的商店。

只需使用 axios 来获取您的资源即可。

axios.get('/testimonial/get/' + id)
  .then(function (response) {
    this.testimonialForm = response.data.testimonial
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });

您可以使用以下输入:

  <el-input
    v-model="testimonialForm.name"
    :placeholder="$t('form.name')"
    name="name"
    type="text"
  />

然后通过 axios 将您的推荐表发送到您的后端。

您可以将underscorejs添加到您的项目中并使用此功能

_.pick(testimonialForm, 'name', 'otherField');

First of all, You have to fetch your object from backend. You do not neet to your store.

Just use axios to fetch your resource.

axios.get('/testimonial/get/' + id)
  .then(function (response) {
    this.testimonialForm = response.data.testimonial
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });

You can use your inputs like:

  <el-input
    v-model="testimonialForm.name"
    :placeholder="$t('form.name')"
    name="name"
    type="text"
  />

Then send your testimonialForm to your backend via axios.

You can add underscorejs to your project and use this function

_.pick(testimonialForm, 'name', 'otherField');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文