首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Vue 3中的Axios观察、比较和发布更新后的表单数据到API

使用Vue 3中的Axios观察、比较和发布更新后的表单数据到API
EN

Stack Overflow用户
提问于 2022-06-05 05:53:09
回答 1查看 550关注 0票数 0

我需要帮助来完成我的代码。

这就是我们所做的。

我正在从API中获取选项,所以我已经将初始状态定义为空。当我从API获得响应时,我更新options.

  • My表单的状态,一旦我从API获得响应,就会显示表单的状态。
  1. 现在使用v绑定,我正在绑定表单.

我需要帮助的地方。

  1. 我需要注意形式上的变化。如果表单元素的值与API响应的状态不同,我希望启用提交按钮。当单击“保存”按钮时,我需要筛选已更改的选项&将该表单数据提交给名为updateOptions.

的pinia操作。

注意: API以这种方式处理post数据。示例:enable_quick_view: true

提前谢谢你。

options.js pinia 商店

代码语言:javascript
复制
import { defineStore } from 'pinia'
import Axios from 'axios';
import axios from 'axios';

const BASE_API_URL = adfy_wp_locolizer.api_url;

export const useOptionsStore = defineStore({

    id: 'Options',

    state: () => ({
        allData: {},
        options: {
            enable_quick_view: null, // boolean
            quick_view_btn_label: "", // string
            quick_view_btn_position: "", // string
        },
        newOptions: {}, // If required, holds the new options to be saved.
        message: "", // Holds the message to be displayed to the user.
        isLoading: true,
        isSaving: false,
        needSave: false,
        errors: [],
    }),
    getters: {

        // ⚡️ Return state of the options.
        loading: (state) => {

            return state.isLoading;
        },
    },
    actions: {

        // ⚡️ Use Axios to get options from api.
        fetchOptions() {

            Axios.get(BASE_API_URL + 'get_options')
                .then(res => {
                    this.alldata = res.data.settings;
                    let settings = res.data.settings_values;

                    /*
                    * Set options state.
                    */
                    this.options.enable_quick_view = JSON.parse(
                        settings.enable_quick_view
                    );
                    this.options.quick_view_btn_label =
                        settings.quick_view_btn_label;

                    this.options.quick_view_btn_position = settings.quick_view_btn_position;

                    /*
                    * End!
                    */

                    this.isLoading = false;
                })
                .catch(err => {

                    this.errors = err;
                    console.log(err);
                })
                .finally(() => {

                    // Do nothing for now.
                });
        },

        // ⚡️ Update options using Axios.
        updateOptions() {

            this.isSaving = true;

            axios.post(BASE_API_URL + 'update_options', payload)
                .then(res => {

                    this.needSave = false;
                    this.isSaving = false;
                    this.message = "Options saved successfully!";
                })
                .catch(err => {

                    this.errors = err;
                    console.log(err);
                    this.message = "Error saving options!";
                })
        }
    },
});

Option.vue组件

代码语言:javascript
复制
<script setup>
    import { onMounted, watch } from "vue";
    import { storeToRefs } from "pinia";
    import { Check, Close } from "@element-plus/icons-vue";
    import Loading from "../Loading.vue";
    import { useOptionsStore } from "../../stores/options";
    let store = useOptionsStore();
    let { needSave, loading, options, newOptions } = storeToRefs(store);

    watch(
        options,
        (state) => {
            console.log(state);
            // Assign the option to the newOptions.
        },
        { deep: true, immediate: false }
    );

    onMounted(() => {
        store.fetchOptions();
    });
</script>
<template>
    <Loading v-if="loading" />
    <form
        v-else
        id="ui-settings-form"
        class="ui-form"
        @submit="store.updateOptions()"
    >
        <h3 class="option-box-title">General</h3>
        <div class="ui-options">
            <div class="ui-option-columns option-box">
                <div class="ui-col left">
                    <div class="label">
                        <p class="option-label">Enable quick view</p>
                        <p class="option-description">
                            Once enabled, it will be visible in product catalog.
                        </p>
                    </div>
                </div>
                <div class="ui-col right">
                    <div class="input">
                        <el-switch
                            v-model="options.enable_quick_view"
                            size="large"
                            inline-prompt
                            :active-icon="Check"
                            :inactive-icon="Close"
                        />
                    </div>
                </div>
            </div>
        </div>
        <!-- // ui-options -->
        <div class="ui-options">
            <div class="ui-option-columns option-box">
                <div class="ui-col left">
                    <div class="label">
                        <p class="option-label">Button label</p>
                    </div>
                </div>
                <div class="ui-col right">
                    <div class="input">
                        <el-input
                            v-model="options.quick_view_btn_label"
                            size="large"
                            placeholder="Quick view"
                        />
                    </div>
                </div>
            </div>
        </div>
        <!-- // ui-options -->
        <button type="submit" class="ui-button" :disabled="needSave == true">
            Save
        </button>
    </form>
</template>
<style lang="css" scoped>
    .el-checkbox {
        --el-checkbox-font-weight: normal;
    }
    .el-select-dropdown__item.selected {
        font-weight: normal;
    }
</style>
EN

回答 1

Stack Overflow用户

发布于 2022-06-05 06:57:32

在watch函数中,您可以比较新值和旧值。但你得把它改成:

代码语言:javascript
复制
watch(options, (newValue, oldValue) => {
   console.log(oldValue, newValue);
   // compare objects
}, {deep: true, immediate: false};

现在,您可以比较旧的和新的对象。我认为谷歌搜索可以帮助你做到这一点。

希望这能有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72505102

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档