javascript - Vue:修改数据返回依赖 userAgent
问题描述
我是VUE的新手,我尝试修改禁用值取决于userAgent显示或隐藏paymentMethod:
data() {
return {
paymentMothods: [
{ name: 'Visa checkout', img: 'visa.png', disabled: false, height: '19', class: 'v-button' },
{ name: 'PayPal', img: 'paypal.png', disabled: false, height: '18.9', class: '' },
{ name: 'PhonePE', img: 'phonepe.png', disabled: true, height: '18.9', class: 'phonepe' },
]
}
},
如果 userAgent 是phonepe-webview我尝试更改值:
methods: {
phopepeMatch: function () {
let userAgent = navigator.userAgent
let phonepeMatch = userAgent.match("phonepe-webview")
if (phonepeMatch === "phonepe-webview"){
this.paymentMothods[2].disabled = false
return true
}
else {
return false
}
}
},
但这并没有显示付款方式:(
解决方案
关于match
返回的内容存在误解。在匹配的情况下,它返回一个数组,而不是匹配的字符串。如果失败,它会返回null
。更多信息在这里。
您可以更新代码以解决此问题:
phopepeMatch: function () {
let userAgent = navigator.userAgent
let phonepeMatch = userAgent.match("phonepe-webview")
if (phonepeMatch === null){
return false
}
else {
this.paymentMothods[2].disabled = false
return true
}
}
推荐阅读
- xamarin - Xamarin iOS: Replace View Controller within the current Tab Bar item
- python - 如何使用 Unix 时间戳向我的 pandas 数据框添加年份和月份列?
- php - 当我将 Drupal 站点移动到网络主机时,所有链接都给出 404 Not Found
- python - 从字典列表中获取所有最小值
- sql - 在 Db2 (SQL/XML) 中使用 xmlquery 从 xml 中选择元素
- .net-core - 如何从 blazor .NET Core 3 项目生成报告?
- regex - 具有单个正则表达式模式的多个嵌套组
- kubernetes - 部署亲和性
- python - 如何使用现有的 python 脚本处理多个文件
- vaex - 使用 Vaex 的性能提示