javascript - Vue:当数据异步更改时 Radio 不会更新其值
问题描述
我不明白为什么我的新代码不起作用。我能够提取一个最小的可重现案例。当created()
同步设置数据时,效果很好,并显示文章收音机。当我用超时包围它时,博客将保持选中状态。Vue 2.6.12
此代码中的错误已修复,但这不是我麻烦的原因,因为我的真实代码不同。我的问题是,在更改反应数据之后,未选中单选按钮。
<Radio
v-model="type"
identifier="article"
class="pl-3"
label="article"
name="type"
/>
<Radio
v-model="type"
identifier="blog"
class="pl-3"
label="blog"
name="type"
/>
<div>Selected {{ type }}</div>
data() {
return {
type: "blog",
};
},
created() {
setTimeout(function () {
this.type = "article";
console.log(this.type);
}, 800);
},
这让我大吃一惊,因为不同组件中的类似代码运行良好。
更新:
我的原始代码不起作用,是
computed: {
blog() {
return this.$store.getters.BLOG;
},
},
watch: {
blog() {
this.type = (this.blog.info.editorial) ? 'article' : 'blog';
},
created() {
this.$store.dispatch('FETCH_BLOG', { slug: this.slug });
},
相关源代码:
解决方案
您只需将函数更改为箭头函数,因为它不会像这样指向您的数据
setTimeout(() => {
this.type = "article";
console.log(this.type);
}, 800);
推荐阅读
- mongodb - Golang MGO 模块是否锁定或解锁 Go 对象?
- php - php循环中的第一个选择即将结束
- vsphere - Vsphere html 网页客户端 6.7 sdk
- android - Android:检测应用程序的内存是否已被修改
- xml - 如何使用 ASP.NET 从 XML 文件中检索特定数据
- html - 带有 vmin 和移动地址栏的响应式设计
- asp.net - 更改 ASP.NET Core Web 应用中的启动错误页面
- google-cloud-platform - 是否跨项目计算持续使用折扣?
- node.js - 如何在 Firebase 函数中与 firestore + pdfmake 一起使用 Promise
- java - 如何将网页保存为 Web 存档,然后使用 Android 的 WebView 将 Web 存档显示为网页?