【问题标题】:VueJs fetching questions in Quiz appVueJs 在测验应用程序中获取问题
【发布时间】:2018-08-08 06:51:39
【问题描述】:

我正在尝试构建一个简单的测验应用程序。它有一些在数据库中的问题。我正在使用 AXIOS 请求获取那些。 我正在做这样的事情:

var app = new Vue({
        el:"#app",
            data:{
                currentQuestion:0,
                question:{}
            },
            methods:{
                    next:function(){
                        this.currentQuestion+=1;
                        this.loadQuest();
                    } , 
                    loadQuest:function(){
                         axios.get('/questions').then((response)=>{
                        //console.log(response.data[this.currentQuestion]);
                        this.question = response.data[this.currentQuestion];
                       })
                    }
            },
            mounted(){
                    this.loadQuest();
                },     
    });

在这里,您可以看到每当我单击下一个问题按钮时,都会调用 loadQuest() 并向服务器发送请求。有没有什么办法不在每次点击下一个按钮时发送请求,而只是增加 currentQuestion 变量并加载下一个问题?

【问题讨论】:

    标签: javascript php vue.js vuejs2 vue-component


    【解决方案1】:
    1. 将“问题”设为数组(而非对象)
    2. 一开始就搞定所有问题
    3. 计算值 'question' 将自动观察 'currentQuestion' 的变化并相应地更新值
    const app = new Vue({ 埃尔:'#app', 数据: { 当前问题:0, 问题: [], }, 挂载(){ axios.get('/questions').then((response) => { // console.log(response.data[this.currentQuestion]); this.questions = response.data }) }, 方法: { 下一个 () { this.currentQuestion += 1 } }, 计算:{ 题 () { 返回 this.questions.length ? this.questions[this.currentQuestion] : {} } } })

    【讨论】:

    • 感谢@sebestian 的回答。代码工作正常,但在控制台中有 2 个警告。 The computed property "question" is already defined in data.[Vue warn]: Error in render: "TypeError: Cannot read property 'question' of undefined"
    • 有什么想法吗?为什么我会收到此警告?
    • 我已经更新了代码,第一个错误应该被删除了。您如何显示“问题”? {{question}} ?
    • 我在数据库中有名为问题的列
    • 哎呀!我犯了一个小错误,这就是它抛出错误的原因。显示这样的问题:

      解决了问题。谢谢,伙计!
    【解决方案2】:

    取决于您有多少问题,但您可以通过加载请求获取所有问题,并将它们存储在数据中声明的questions: {} 对象中。点击后直接从对象中获取问题。下面的代码示例:

    var app = new Vue({
        el:"#app",
            data:{
                currentQuestion:0,
                questions:{},
                question:{}
            },
            methods:{
                    next:function(){
                        this.loadNextQuest();
                        this.currentQuestion+=1;
                    } , 
                    loadQuest:function(){
                         axios.get('/questions').then((response)=>{
                        //console.log(response.data[this.currentQuestion]);
                        this.questions = response.data;
                       })
                    },
                    loadNextQuest:function(){
                        this.question = this.questions[this.currentQuestion];
                    }
            },
            mounted(){
                    this.loadQuest();
                },     
    });
    

    【讨论】:

    • 感谢您的回答。这对我来说可以。但在控制台中出现以下错误。 app.js:32494 [Vue 警告]:渲染错误:“TypeError:无法读取未定义的属性‘问题’”
    • 在 HTML 中,我在数据库列名称中将这些值称为 <h4 v-text="questions[currentQuestion].question"></h4> 是问题
    猜你喜欢
    • 2023-04-08
    • 1970-01-01
    • 2011-05-13
    • 1970-01-01
    • 1970-01-01
    • 2022-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多