gpt4 book ai didi

javascript - 值未显示且Vue.js控制台中出现错误(laravel6)

转载 作者:行者123 更新时间:2023-12-03 06:45:53 25 4
gpt4 key购买 nike

我在显示vue.js中的数据时出错
这是错误。

the error

这是vue.js结果

enter image description here

我在counter1中有一个数据,其余的都没有任何值。
但是我希望它在没有值的情况下显示空白,并显示已经具有值的数据。

这是我的vue.js脚本:

<script>
const app = new Vue({
el:'#app',
data:{
queue:{},

},
mounted(){
this.getQueue();
},
methods:{
getQueue(){
axios.get('api/display/showqueue')
.then((response)=>{
this.queue=response.data
})

.catch(function (error){
console.log(error);
});
}
}

})
</script>

这是我的vue.js数据输出的HTML:
<div id="app" class="row">

<div class="col-sm-6">
<div class="card bg-gradient-lighter mt-3 shadow">
<div class="card-header-lg">
<h3 class="text-default text-uppercase" v-if="queue.cashier1.department">@{{ (queue.cashier1 || {}).department}}</h3>
<h3 class="text-default text-uppercase" v-else>-</h3>
</div>
<div class="card-body-sm">
<h1 class="display-1 font-weight-bold" v-if="queue.cashier1"><strong>@{{ (queue.cashier1 || {}).letter}}-@{{ (queue.cashier1 || {}).number}}</strong></h1>
</div>
<div class="card-footer-sm">
<p class="text-warning font-weight-bold">COUNTER 1</p>
</div>
</div>
</div>
<div class="col-sm-6">
<div class="card bg-gradient-lighter shadow mt-3">
<div class="card-header-lg">
<h3 class="text-default text-uppercase" v-if="queue.cashier2.department">@{{ (queue.cashier2 || {}).department}}</h3>
<h3 class="text-default text-uppercase" v-else>-</h3>
</div>
<div class="card-body-sm">
<h1 class="display-1 font-weight-bold" v-if="queue.cashier2"><strong>@{{ (queue.cashier2 || {}).letter}}-@{{ (queue.cashier2 || {}).number}}</strong></h1>
</div>
<div class="card-footer-sm">
<p class="text-warning font-weight-bold">COUNTER 2</p>
</div>
</div>
</div>


<div class="col-sm-6">
<div class="card bg-gradient-lighter shadow mt-3">
<div class="card-header-lg">
<h3 class="text-default text-uppercase" v-if="queue.accounting1.department">@{{ (queue.accounting1 || {}).department}}</h3>
<h3 class="text-default text-uppercase" v-else>-</h3>
</div>
<div class="card-body-sm">
<h1 class="display-1 font-weight-bold" v-if="queue.accounting1"><strong>@{{ (queue.accounting1 || {}).letter}}-@{{ (queue.accounting1 || {}).number}}</strong></h1>
</div>
<div class="card-footer-sm">
<p class="text-warning font-weight-bold">COUNTER 1</p>
</div>
</div>
</div>


<div class="col-sm-6">
<div class="card bg-gradient-lighter mt-3 shadow">
<div class="card-header-lg">
<h3 class="text-default text-uppercase" v-if="queue.accounting2.department">@{{ (queue.accounting2 || {}).department}}</h3>
<h3 class="text-default text-uppercase" v-else>-</h3>
</div>
<div class="card-body-sm">
<h1 class="display-1 font-weight-bold" v-if="queue.accounting2"><strong>@{{ (queue.accounting2 || {}).letter}}-@{{ (queue.accounting2 || {}).number}}</strong></h1>
</div>
<div class="card-footer-sm">
<p class="text-warning font-weight-bold">COUNTER 2</p>
</div>
</div>
</div>


<div class="col-sm-6">
<div class="card bg-gradient-lighter shadow mt-3">
<div class="card-header-lg">
<h3 class="text-default text-uppercase" v-if="queue.registrar1.department">@{{ (queue.registrar1 || {}).department}}</h3>
<h3 class="text-default text-uppercase" v-else>-</h3>
</div>
<div class="card-body-sm">
<h1 class="display-1 font-weight-bold" v-if="queue.registrar1"><strong>@{{ (queue.registrar1 || {}).letter}}-@{{ (queue.registrar1 || {}).number}}</strong></h1>
</div>
<div class="card-footer-sm">
<p class="text-warning font-weight-bold">COUNTER 1</p>
</div>
</div>
</div>

<div class="col-sm-6">
<div class="card bg-gradient-lighter mt-3">
<div class="card-header-lg">
<h3 class="text-default text-uppercase" v-if="queue.registrar2.department">@{{ (queue.registrar2 || {}).department}}</h3>
<h3 class="text-default text-uppercase" v-else>-</h3>
</div>
<div class="card-body-sm">
<h1 class="display-1 font-weight-bold" v-if="queue.registrar2"><strong>@{{ (queue.registrar2 || {}).letter}}-@{{ (queue.registrar2 || {}).number}}</strong></h1>
</div>
<div class="card-footer-sm">
<p class="text-warning font-weight-bold">COUNTER 2</p>
</div>
</div>
</div>

</div>

如何确保只要有可用数据,它就会显示在页面上,但是如果我没有数据。它将保留为空白并且没有错误。

最佳答案

之所以遇到此错误,是因为您尝试访问undefined的属性,因为queue最初为空,并且queue.cashier1和所有其他属性未定义。因此,可以通过添加检查是否正在加载数据以及是否没有错误获取结果的方法来尝试此操作。

<div id="app">
<div v-if="!isLoading && !isError" class="row">
<div class="col-sm-6">
<div class="card bg-gradient-lighter mt-3 shadow">
<div class="card-header-lg">
<h3 class="text-default text-uppercase" v-if="queue.cashier1 && queue.cashier1.department">@{{ (queue.cashier1 || {}).department}}</h3>
<h3 class="text-default text-uppercase" v-else>-</h3>
</div>
<div class="card-body-sm">
<h1 class="display-1 font-weight-bold" v-if="queue.cashier1"><strong>@{{ (queue.cashier1 || {}).letter}}-@{{ (queue.cashier1 || {}).number}}</strong></h1>
</div>
<div class="card-footer-sm">
<p class="text-warning font-weight-bold">COUNTER 1</p>
</div>
</div>
</div>
<div class="col-sm-6">
<div class="card bg-gradient-lighter shadow mt-3">
<div class="card-header-lg">
<h3 class="text-default text-uppercase" v-if="queue.cashier2 && queue.cashier2.department">@{{ (queue.cashier2 || {}).department}}</h3>
<h3 class="text-default text-uppercase" v-else>-</h3>
</div>
<div class="card-body-sm">
<h1 class="display-1 font-weight-bold" v-if="queue.cashier2"><strong>@{{ (queue.cashier2 || {}).letter}}-@{{ (queue.cashier2 || {}).number}}</strong></h1>
</div>
<div class="card-footer-sm">
<p class="text-warning font-weight-bold">COUNTER 2</p>
</div>
</div>
</div>

<div class="col-sm-6">
<div class="card bg-gradient-lighter shadow mt-3">
<div class="card-header-lg">
<h3 class="text-default text-uppercase" v-if="queue.accounting1 && queue.accounting1.department">@{{ (queue.accounting1 || {}).department}}</h3>
<h3 class="text-default text-uppercase" v-else>-</h3>
</div>
<div class="card-body-sm">
<h1 class="display-1 font-weight-bold" v-if="queue.accounting1"><strong>@{{ (queue.accounting1 || {}).letter}}-@{{ (queue.accounting1 || {}).number}}</strong></h1>
</div>
<div class="card-footer-sm">
<p class="text-warning font-weight-bold">COUNTER 1</p>
</div>
</div>
</div>

<div class="col-sm-6">
<div class="card bg-gradient-lighter mt-3 shadow">
<div class="card-header-lg">
<h3 class="text-default text-uppercase" v-if="queue.accounting2 && queue.accounting2.department">@{{ (queue.accounting2 || {}).department}}</h3>
<h3 class="text-default text-uppercase" v-else>-</h3>
</div>
<div class="card-body-sm">
<h1 class="display-1 font-weight-bold" v-if="queue.accounting2"><strong>@{{ (queue.accounting2 || {}).letter}}-@{{ (queue.accounting2 || {}).number}}</strong></h1>
</div>
<div class="card-footer-sm">
<p class="text-warning font-weight-bold">COUNTER 2</p>
</div>
</div>
</div>

<div class="col-sm-6">
<div class="card bg-gradient-lighter shadow mt-3">
<div class="card-header-lg">
<h3 class="text-default text-uppercase" v-if="queue.registrar1 && queue.registrar1.department">@{{ (queue.registrar1 || {}).department}}</h3>
<h3 class="text-default text-uppercase" v-else>-</h3>
</div>
<div class="card-body-sm">
<h1 class="display-1 font-weight-bold" v-if="queue.registrar1"><strong>@{{ (queue.registrar1 || {}).letter}}-@{{ (queue.registrar1 || {}).number}}</strong></h1>
</div>
<div class="card-footer-sm">
<p class="text-warning font-weight-bold">COUNTER 1</p>
</div>
</div>
</div>

<div class="col-sm-6">
<div class="card bg-gradient-lighter mt-3">
<div class="card-header-lg">
<h3 class="text-default text-uppercase" v-if="queue.registrar2 && queue.registrar2.department">@{{ (queue.registrar2 || {}).department}}</h3>
<h3 class="text-default text-uppercase" v-else>-</h3>
</div>
<div class="card-body-sm">
<h1 class="display-1 font-weight-bold" v-if="queue.registrar2"><strong>@{{ (queue.registrar2 || {}).letter}}-@{{ (queue.registrar2 || {}).number}}</strong></h1>
</div>
<div class="card-footer-sm">
<p class="text-warning font-weight-bold">COUNTER 2</p>
</div>
</div>
</div>
</div>
<div class="row" v-else>
<span v-if="isLoading">Loading...</span>
<span v-else>Error loading data...</span>
</div>
</div>

脚本:
<script>
const app = new Vue({
el:'#app',
data:{
queue:{},
isLoading: false,
isError: false

},
mounted(){
this.getQueue();
},
methods:{
getQueue(){
this.isLoading = true;
this.isError = false
axios.get('api/display/showqueue')
.then((response)=>{
this.queue=response.data
this.isLoading = false
})

.catch(function (error){
console.log(error);
this.isLoading = false
this.isError = true
});
}
}

})
</script>

关于javascript - 值未显示且Vue.js控制台中出现错误(laravel6),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60349394/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com