[Vue警告]:渲染错误:" TypeError:对象未定义"

时间:2018-06-09 08:27:07

标签: javascript vue.js vuejs2



<script>
        const app = new Vue({
            el: '#app', 
            data:{
                results:{}
            },
            mounted()
            {
                axios.get('{{ route('request.data') }}').then(response=>this.results = response.data)
            }
        });
    </script>
&#13;
<span class="company-value" v-text="results.request_stats.new"></span>
&#13;
&#13;
&#13;

在标题中,这是我得到的错误。但当我console.log(object)时,值就在那里而且它不是空的。那么这个错误的重点是什么

注意:object = results.request_stats

2 个答案:

答案 0 :(得分:0)

您必须声明并指定一些值{}[]来绑定,否则您将获得request_stats.new

的未定义错误

const app = new Vue({
            el: '#app', 
            data:{
                results:{request_stats:{}},
                loading:true
            },
            mounted()
            {
              var vm = this;
               setTimeout(function(){ 
                vm.loading = false;
                vm.results.request_stats.new = "Niklesh Raut";
               }, 1000);
            }
        });
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.js"></script>
<div id="app">
<div>
<span class="company-value" v-text="results.request_stats.new"></span>
<div v-show="loading">loading...</div>
</div>
</div>

没有分配,哪个给出错误

const app = new Vue({
            el: '#app', 
            data:{
                results:{},
                loading:true
            },
            mounted()
            {
              var vm = this;
               setTimeout(function(){ 
                vm.loading = false;
                vm.results.request_stats.new = "Niklesh Raut";
               }, 1000);
            }
        });
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.js"></script>
<div id="app">
<div>
<span class="company-value" v-text="results.request_stats.new"></span>
<div v-show="loading">loading...</div>
</div>
</div>

答案 1 :(得分:0)

一种解决方法是将以下代码段添加到您的跨度中

v-if="results && results.request_stats"