javascript - Vue.JS:除非我打开开发工具,否则变量不会更新
问题描述
我有两个按钮,它们通过 v-if 标签根据变量显示。如果变量为假,则显示一个按钮,如果为真,则显示另一个按钮。
bool 变量仅在我在 chrome 中打开开发工具时才会更新……我不知道为什么会这样。
视频:https ://i.gyazo.com/95a90354fdcec849cc7e33aaa5e1d8b9.mp4
最后一帧截图:https ://gyazo.com/ecc0cbfbc01cf6f472cea48c6cc89a4a
按钮代码
<div class="form-row float-right">
<button v-if="btnDisabled" class="btn btn-success" disabled style="margin-top: 25px;">
Save
</button>
<button v-else class="btn btn-success" style="margin-top: 25px;"
v-on:click="createAlert()">
Alert me when {{desiredPriceComputed}}
</button>
</div>
我的一些数据变量……</p>
btnDisabled: true,
inputInvalid: false,
hideProduct: false,
hidePriceForm: false,
busy: false,
desiredPriceComputed 的函数(其中按钮变量从 true 更改为 false。
desiredPriceComputed: function () {
let input = this.desiredPriceInput;
let pInput = parseFloat(this.desiredPriceInput);
if (input == "") {
this.inputInvalid = false;
this.btnDisabled = true;
this.cardAlert.desiredPrice = 0;
return '';
}
if (Number.isNaN(pInput)) {
this.inputInvalid = true;
this.btnDisabled = true;
this.cardAlert.desiredPrice = 0;
return '';
}
if (pInput < 0) {
this.inputInvalid = true;
this.btnDisabled = true;
this.cardAlert.desiredPrice = 0;
return '';
}
if (pInput > 1000000000) {
this.inputInvalid = true;
this.btnDisabled = true;
this.cardAlert.desiredPrice = 0;
return '';
}
this.inputInvalid = false;
this.btnDisabled = false;
this.cardAlert.desiredPrice = pInput.toFixed(2);
if (pInput > this.cardAlert.currentPrice) {
return '$' + pInput.toFixed(2) + " or higher";
}
else if (pInput < this.cardAlert.currentPrice) {
return '$' + pInput.toFixed(2) + " or lower";
}
else {
return '$' + pInput.toFixed(2);
}
}
其他一些重要说明
我将 Vue 单文件组件与 ASP.NET MVC 剃须刀页面一起使用。
我目前正在通过标题标签内的以下内容调用 Vue。
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script>
解决方案
我认为这是摘要周期的问题,在 desiredPriceComputed 中尝试用 this.$nextTick(() => {...}) 或简单的 setTimeout(fn, 0) 包装它,如果它有效,请分享更多细节或尝试更改按钮的更新方式(使用一个按钮而不是 2 个)
推荐阅读
- excel - 无法在 VBA 中脱机打开 OneDrive 文件
- r - 循环遍历 R 中的行和列不起作用
- php - 包含或需要文件时设置显式 href 目录
- python - 在熊猫中创建日期范围对
- java - InetAddress.getLocalHost().toString() 返回不同的 ip 地址
- firebase - 从 Firestore 中的另一个文档列表中排除列表 - Swift
- python - 将字符串转换为 NumpyArray
- c - 如何重复一段C代码,每次只改变几个关键字
- sql - 如何将 ColdFusion LDAP 查询 .cfm 文件中的数据导入 SQL 表?
- sql - 将某个表中的某些内容插入到另一个表中