我试图根据API响应设置复选框的状态,但无法更改其状态.

App.vue

<script setup>
    import { ref, toRefs, reactive, onMounted } from "vue";
    import axios from "axios";
    import { Check, Close } from "@element-plus/icons-vue";
    import Loading from "../Loading.vue";

    let state = reactive({
        options: {},
        errors: [],
        loading: true,
        enableQuickView: true, // ???????? How to replace this value based on the API response?
    });

    let { loading, options, enableQuickView } = toRefs(state);

    let BASE_API_URL = 'http://wpvue.local/wp-json/quick_view_api/get_settings/';

    // ???? Doing API call to get the settings.
    let FetchOptions = () => {
        axios
            .get(BASE_API_URL + "get_options")
            .then((res) => {
                state.options = res.data.settings;
                console.log(res.data.settings);
                state.loading = false;
            })
            .catch((err) => {
                console.log(err);
                state.errors = err;
            });
    };

    // ???? Invoke FetchOptions function.
    onMounted(() => {
        FetchOptions();
    });
</script>
<template>
    <Loading v-if="loading" />
    <form v-else id="xox-settings-form" class="xox-form" @submit.prevent>
        <h3 class="option-box-title">{{ options.general.section_label }}</h3>
        <div class="input">
            <el-switch
                v-model="enableQuickView"
                class="enable-addonify-quick-view"
                size="large"
                inline-prompt
                :active-icon="Check"
                :inactive-icon="Close"
            />
        </div>
    </form>
</template>

API response:

{
    "settings": {
        "general": {
            "section_label": "General",
            "section_fields": {
                "enable_quick_view": {
                    "label": "Enable Quick View",
                    "description": "",
                    "type": "checkbox",
                    "value": "true"
                },
                "quick_view_btn_position": {
                    "label": "Quick View Button Position",
                    "description": "",
                    "type": "select",
                    "value": "",
                    "choices": {
                        "after_add_to_cart_button": "After Add to Cart Button",
                        "before_add_to_cart_button": "Before Add to Cart Button"
                    }
                }
            }
        }
    }
}

如果你看看这个应用程序.我正在使用的vue模板{{ options.general.section_label }};它工作得很好.这是一个章节标题&amp;我不需要任何react 状态.

但是,对于像checkbox & select这样的元素,我希望它们是react 性的,以便稍后我可以看到用户更改的状态&amp;稍后将其更新到API.

非常感谢.

推荐答案

try 将值转换为布尔值:

...
state.options = res.data.settings;
state.enableQuickView = 
  state.options.general.section_fields.enable_quick_view.value === 'true'
...

Vue.js相关问答推荐

复选框列未在vutify中排序

VUE:带自定义组件的数组输入绑定

更新 Shopware 管理 CMS 组件中的编辑器视图

Vuetify 3 改变 Select 的 colored颜色

如何在Vue3中使用vuechartjs 5.2从父组件更新图表

使用 nuxt.js 组件自动导入对性能不利吗?

在 Vue 中启用(控制台)记录路由事件

VueJS:在组件之间使用全局对象的最佳实践?

如何在 Vuetify v-data-table 上对齐标题

如何为 Vue 应用提供 robots.txt

加载和渲染时对象值上的 Vue.js 未定义错误

事件 click点击的无效处理程序:未定义

VueRouter 未定义

输入文件 Select 事件在 Select 同一文件时未触发

'不提供名为'createRouter'的导出'' vue 3、vite 和 vue-router

使用 Firebase Auth for Google 将匿名用户转换为注册用户

克隆元素并附加到 DOM 的正确方法

使用 vuejs 动态设置 id 标签

在 Vue 应用程序中创建一个类的单个实例

在~/components/AddPlaceModal.vue中找不到导出AddPlaceModal