javascript - VueJS:根据查询变量更新数据
问题描述
在我的路线中,我有一个参数,例如: ?id=101
我有一些数据,例如:
data () {
return {
record: {id: null}
}
}
现在,我想做的是:如果存在查询参数,我想用查询变量中的 id 更新数据中的 id。我尝试在 fetch 中执行此操作,如下所示:
async fetch ({ store, redirect, params, query }) {
this.record = {id: query.id}
}
然而,什么也没有发生。我也尝试从 中调用一个函数fetch
,但它说该方法未定义。
你能帮忙吗?
解决方案
好的,我的答案被删除了。在下面找到答案:
export default {
data () {
return {
record: {
id: null
}
}
},
created () {
this.fetchData()
},
watch: {
'$route': 'fetchData'
},
methods: {
fetchData () {
getRecord(this.$route.query.id, (err, id) => {
if (err) {
this.error = err.toString()
} else {
this.record.id = id
}
})
}
}
}
更多信息在这里:
推荐阅读
- sql - TDE 我们是否需要在 DR 服务器/Test Server 上备份服务主密钥 (SMK) 和数据库主密钥 (DMK)
- sql - 用纯文本替换 unicode 字符的 SQL 命令
- asp.net-core - HubContext的单元测试.net core 2.2 SignalR集线器问题
- c++ - 更新的包,现在用 -O0 编译的 Mex 文件是“无效的 mex 文件”?
- apache-kafka - 使用窗口翻转时如何从 KSQL 流中获取最后一个值
- google-bigquery - 将数据流式传输到 Google BigQuery 表中:使用 InsertId 去重复记录的问题
- php - PHP 回声循环正在破坏 CSS 弹性框
- r - 如何按秒间隔对数据进行分组?
- excel - 粘贴或删除时出现 VBA Vlookup 运行时错误
- c++ - 自定义char二维数组排序函数