【发布时间】:2020-08-17 12:53:37
【问题描述】:
这是我的rightTableMenu 模板
<template>
<div>
<h1 align="center">{{ title }}</h1>
<v-alert type="info" icon="mdi-emoticon-sad" v-if="basketStatus">
Empty Basket, please add some to basket
</v-alert>
<div v-if="changeAlertStatus()">
<v-alert
type="success"
icon="mdi-emoticon-happy"
:value="alert"
transition="fade-transition"
>
thank you
</v-alert>
<v-simple-table>
<template v-slot:default>
<thead>
<tr>
<th class="text-left">Quantity</th>
<th class="text-left">Name</th>
<th class="text-left">Price</th>
</tr>
</thead>
<tbody>
<tr v-for="item in basket" :key="item.name">
<td>
<v-icon @click="increaseQuantity(item)">add_box</v-icon>
<span>{{ item.quantity }}</span>
<v-icon @click="decreaseQuantity(item)"
>indeterminate_check_box
</v-icon>
</td>
<td>{{ item.name }}</td>
<td>{{ (item.price * item.quantity).toFixed(2) }}</td>
</tr>
</tbody>
</template>
</v-simple-table>
<v-divider color="black"></v-divider>
<v-row id="basket_checkout" style="margin: 0">
<v-col>
<p>Subtotal:</p>
<p>Delivery:</p>
<p>Total amount:</p>
</v-col>
<v-col class="text-right">
<p>${{ subTotalResult }}</p>
<p>$10</p>
<p class="font-weight-bold">${{ totalPriceResult }}</p>
</v-col>
</v-row>
<v-row>
<v-spacer></v-spacer>
<v-btn depressed class="orange" v-on:click="submitOrder">
<v-icon>shopping_basket</v-icon>
</v-btn>
</v-row>
</div>
</div>
</template>
如您所见,通过检查以下内容,当阵列篮内没有物品时,会显示两个警报
basketStatus() {
return this.$store.getters.basket.length === 0;
},
这是计算属性 我的数据属性部分是
data() {
return {
title: "Current Basket",
alert: false,
};
},
但是对于第二个 v-alert,我想让警报在几秒钟后显示并消失,到目前为止我已经为它做了以下操作
async changeAlertStatus() {
if (this.$store.getters.basket.length !== 0) {
this.alert = true;
try {
const response = await setTimeout(() => {
this.alert = false;
}, 100);
console.log("this is the resonse " + response);
} catch (err) {
console.log("fetch failed", err);
}
} else {
this.alert = false;
}
},
这是一种方法 我很困惑如何在不使用 v-if 指令的情况下插入 div 部分中的函数,并且当我在控制台中检查它并且 v-alert 不会消失时,我的 async changeAlertStatus 进入无限循环
对此有什么想法吗?
如果需要更多信息,请告诉我
谢谢你
以防我的leftTableMenu 被关注
<template>
<div>
<div v-if="showError['situation']">
<!--
basically, when you close the alert, the value of the alert goes to false
so you need to turn it to true when there is an error :value="showError.situation" -->
<app-alert :text="showError.message" :value.sync="showError.situation"></app-alert>
</div>
<h1 align="center">{{ title }}</h1>
<v-simple-table od="menu-table">
<template v-slot:default>
<thead>
<tr>
<th class="text-left">Name</th>
<th class="text-left">Price</th>
<th class="text-left">Add</th>
</tr>
</thead>
<tbody>
<tr v-for="item in menuItems" :key="item.name">
<td>
<span id="id_name">{{ item.name }}</span>
<br />
<span id="menu_item_description">{{ item.description }}</span>
</td>
<td>{{ item.price }}</td>
<td>
<v-btn text v-on:click="addToBasket(item)">
<v-icon color="orange">1add_shopping_cart</v-icon>
<span></span>
</v-btn>
</td>
</tr>
</tbody>
</template>
</v-simple-table>
</div>
</template>
<script>
export default {
name: 'LeftTableMenu',
data() {
return {
title: "Menu Items",
};
},
methods: {
addToBasket(item) {
this.$store.dispatch("addToBasket", item);
},
},
computed: {
showError() {
return this.$store.getters.showError;
},
menuItems() {
return this.$store.getters.menuItems;
},
},
};
【问题讨论】:
-
不确定你为什么在
v-if中调用一个函数(?)changeAlerStatus(),如果它也没有返回任何东西。另外,你真的知道 setTimeout 是做什么的吗?为什么要等待它的回复? -
@A. Lau 我是 vue js 的新手,除了使用 v-if 之外我不知道更好。是的 setTimeout 将任务放在线程的末尾,以便稍后运行。因为我希望它在几秒钟后消失,正如我所提到的。我完全同意 changeAlertStatus 不返回任何内容,但是如何在 div 中使用它来检查数组是否为空?
-
您不需要等待 setTimeout,它不会返回承诺。 w3schools.com/jsref/met_win_settimeout.asp 另外,计算值不是函数,它们只是值,所以不需要调用它们。您的
changeAlertStatus()应在 v-if 中更改为alert。至少应该防止无限循环 -
我怎样才能在几秒钟后消失?
-
把你的 setTimeout 逻辑放到一个观察器中,观察
basketStatusvuejs.org/v2/guide/computed.html#Watchers
标签: javascript vue.js vuetify.js settimeout