vue.js - id passed through params get lost on page refresh
问题描述
I have a component called Subscribers
that receives props via params from another component called Details
. I use the props received to make an API call to get the list of subscribers. This works well.
But an issue arises when the page is refreshed. The id that is passed via params is lost - becomes null. Cause of that subscribers
which is defined as an empty array in data becomes undefined as seen in this error:
TypeError: Cannot read property 'length' of undefined
This is how I am passing the params from Details
component
<router-link class="unsubscribe" :to="{ name: 'fund-subscribers', params: {fundProp: fund, id: fund.id } }">View all subscribers to this fund</router-link>
Then I have this:
props: {
fundProp: {
type: Object,
default() {
return {
id: null,
title: '',
description: ''
};
}
},
},
data() {
return {
fund: this.fundProp,
subscribers: [],
}
},
Here is the route configuration code for fund-subscribers
{
path: 'funds/:id/subscribers',
name: 'fund-subscribers',
component: Subscribers,
meta: {
title: 'Admin'
},
props: true
},
What could I be doing wrong?
解决方案
所以我找到了解决它的方法。首先,没有必要通过fundProp
,我只能将id
via 参数传递给router-link
<router-link class="unsubscribe" :to="{ name: 'fund-subscribers', params: { id: fund.id } }">
View all subscribers to this fund
</router-link>
完成后,我现在可以使用this.$route.params.id
. 我可以在我的组件数据中设置我的资金,然后用它来进行 API 调用。
data() {
return {
fund: this.$route.params.id
}
},
向服务器发出请求的函数可以使用此路径:
/funds/${fund}/users
,而不是/funds/${fund.id}/users
我希望这对以后的人有帮助(将来也对我有帮助)。
为了更好地理解它,请使用以下链接:
https://forum.vuejs.org/t/route-params-not-showing-after-full-page-refresh/30364
推荐阅读
- python - 使用 service_account_file 找不到电子表格
- vba - VBA替换某些马车
- ruby-on-rails - has_many_association 多个查询使其变慢
- neo4j - 撤消 neo4j 模式中的操作
- javascript - 无法在axios成功回调中更新vue对象的属性
- linux - 尝试连接到服务器时,Sshutle 连接被拒绝
- android - Android Room DB:无法运行失效跟踪器。数据库关闭了吗?
- dns - 如何设置您的 DNS 以从您的邮件服务器代表您的其他域发送邮件
- java - 带开关的枚举
- java - 在 Map 中获取前 N 个元素的最佳方法是什么
基于他们的价值观?