简体   繁体   English

VueJS:使用 v-for 渲染数组

[英]VueJS: Rendering array using v-for

From the GET request, I am getting the JOSN response as follows:从 GET 请求中,我得到 JOSN 响应,如下所示:

[{"key":"COMPLAINT","value":"Complaint"},{"key":"DONATE_ENQ","value":"Donation Enquiry"},{"key":"GENERAL_ENQ","value":"General Enquiry"},{"key":"MEMBERSHIP_ENQ","value":"Membership Enquiry"},{"key":"VOL_ENQ","value":"Volunteer Enquiry"}] [{"key":"COMPLAINT","value":"Complaint"},{"key":"DONATE_ENQ","value":"捐赠查询"},{"key":"GENERAL_ENQ","value" :"一般查询"},{"key":"MEMBERSHIP_ENQ","value":"会员查询"},{"key":"VOL_ENQ","value":"志愿者查询"}]

JS code is: JS代码是:

getEnquiry: function getEnquiry() {
                this.applicant1.option_lookup = document.getElementById('hdnOptionsLookup').value;
                var optionLookupName = this.applicant1.option_lookup;
                axios.get("/TESTAPI/Lookup/Enquiry?optionLookupName=" + optionLookupName).then(function (response) {
                    this.applicant1.enquiry = response.data;
                    var test = this.applicant1.enquiry;
                    alert(test);
                    console.log(response.data);
                    this.loading = false;
                }, function (error) {
                    console.log(error);
                    this.loading = false;
                });
            },

The variable in JS is defined as follows: JS中的变量定义如下:

 applicant1: { enquiry: [{ key: "", value: "" },
                        { key: "", value: "" },
                        { key: "", value: "" },
                        { key: "", value: "" },
                        { key: "", value: "" }], 
                 }

I want to render each key and value pairs in my html as follows:我想在我的 html 中呈现每个键和值对,如下所示:

<div class="form-group" v-bind:class="{input_error:applicant1.enquiry_error}">
                                <select id="applicant1_enquiry" class="form-control" v-model="applicant1.enquiry">
                                    <option :value="null">Select the reason for your enquiry</option>
                                    <option v-for="enq in applicant1.enquiry" :value="enq.key">{{enq.value}}</option>
                                </select>
                            </div>

But I am not able to see the values in my drop down.但我无法在下拉列表中看到值。 Can anyone please help?有人可以帮忙吗? Thanks in advance.提前致谢。

It's common reactivity problem.这是常见的反应性问题。 You can try:你可以试试:

var self = this
axios.get("/TESTAPI/Lookup/Enquiry?optionLookupName=" + optionLookupName).then(function (response) {
  self.applicant1.enquiry = response.data
  self.applicant1 = JSON.parse(JSON.stringify(self.applicant1))
  self.loading = false;
}, function (error) {
  console.log(error);
  self.loading = false;
});

Another way is using Vue.set(self.applicant1, 'enquiry', response.data)另一种方法是使用Vue.set(self.applicant1, 'enquiry', response.data)

Another way to optimize your API calls and data management is to use Vuex:另一种优化 API 调用和数据管理的方法是使用 Vuex:

https://vuex.vuejs.org/ https://vuex.vuejs.org/

With Vuex, you can access your API response from multiple components, not only the one in which you performed the call.使用 Vuex,您可以从多个组件访问您的 API 响应,而不仅仅是您执行调用的组件。

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

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