【发布时间】:2020-09-19 15:35:39
【问题描述】:
我正在使用参数从路由器链接传递用户 ID,以在配置文件页面中显示用户的详细信息,并且我正在获取所有必需的数据并且也正确显示,但是一旦我重新加载页面,$ route.params.id 更改为 undefined 并且显示的数据已消失以获取数据,我必须再次导航到索引页面并重新访问链接。
我的带有路由器链接的 index.vue 页面如下
<h6 class="author">
<router-link :to="{name: 'FrontProfile' , params: {
id: post.user.id }}"
tag="a" active-class="active">{{post.user.name}}
</router-link>
</h6>
我的个人资料页面代码是:
data(){
return {
id: this.$route.params.id,
user: {},
thoughts: {},
}
},
methods:{
viewProfile() {
axios.get('user_profile/' + this.id).then((response) => {
this.user = response.data.user_data;
this.thoughts = response.data.thoughts;
}).catch(() => {
});
},
watch:{
'$route' : 'viewProfile'
},
created() {
this.viewProfile();
}
我可以尝试什么来解决这个问题?
【问题讨论】:
-
你能在 CodePen 中重现这个问题吗?其他疑问,重新加载页面后,
id在 URL 中? -
也发布您的路线文件。
-
其他想法:可能
created事件有问题,尝试使用mounted事件。 -
如果在您刷新配置文件 url 上的浏览器时发生这种情况,那么听起来这可能与您如何捕获 url 服务器端有关,也许 id 没有通过那里传递。
-
@GabrielWillemann 我也会按照你的要求在 codepen 中发布问题。
标签: vue.js vuejs2 vue-component vue-router