【问题标题】:The right format of an object for a v-forv-for 对象的正确格式
【发布时间】:2021-08-17 04:16:42
【问题描述】:

我想在melange() 中显示每个随机播放的答案,但reponse 会返回任何内容。我不知道我的 v-for 出了什么问题。可能是我的对象的格式。

             <label v-bind:key="key" class="bg-white block mt-4 border border-gray-300 rounded-lg py-2 px-6 text-lg"
                v-for="reponse,key in reponseQuestion(questions[currentQuestion].idQuestion)" 
                <div class="fas fa-check" v-bind:id="key" 
                v-bind:value="reponse" 
                v-on:click="repondu(key)"
                v-bind:disabled="questionSelectionne != ''">
                    {{ reponse }}
                </div>
            </label>


        reponseQuestion(idQuest) {
            this.$axios
                .get("http://localhost:4000/api/answers/" + idQuest)
                .then((response) => {
                    console.log(this.melange(response.data[0]))
                    return this.melange(response.data[0])
                })
                .catch((error) => {
                    console.log(error);
                })
        },
        melange(obj) {
            // new obj to return
            let newObj = {};
            // create keys array
            var keys = Object.keys(obj);
            // randomize keys array
            keys.sort(function () {
                return Math.random() - 0.5;
            });
            // save in new array
            keys.forEach(function (k) {
                newObj[k] = obj[k];
            });
            return newObj
        }

reponseQuestion() 的结果:

{reponse4: "8 s 36", reponse3: "8 s 42", reponse2: "9 s 72", reponse1: "9 s 58"}

提前谢谢你

【问题讨论】:

    标签: vue.js object vuejs2 v-for


    【解决方案1】:

    您需要在 (response,key) in responseQuestion 周围加上大括号来覆盖运算符优先级。

    【讨论】:

    • 我在 responseQuestion return {newObj} 周围添加了大括号,然后我更改了 v-for reponse,key in reponseQuestion(questions[questionActuel].idQuestion)[newObj] 这就是你的意思?
    • 我的意思是,如果没有大括号,responsekey 之间的逗号运算符的优先级高于in 关键字。
    猜你喜欢
    • 2019-06-04
    • 2021-12-30
    • 2015-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-20
    • 1970-01-01
    • 2020-11-05
    相关资源
    最近更新 更多