可是在mounted打印不到this.feedBackItem,有什么办法吗
时间: 2024-02-24 22:55:27 浏览: 18
在 `beforeRouteEnter` 钩子函数中,组件实例还未被创建,所以无法直接访问组件的数据和方法。为了在组件的 `mounted` 钩子函数中访问到 `feedBackItem` 数据属性,我们可以通过在 `beforeRouteEnter` 钩子函数中调用 `next` 方法时,将 `feedBackItem` 数据属性作为参数传递到回调函数中,然后在回调函数中访问该参数。代码示例如下:
```javascript
mounted() {
this.showFeedbackDrawer();
console.log(this.feedBackItem, '----');
},
beforeRouteEnter(to, from, next) {
if(from.path == '/recovery-list') {
next(vm => {
vm.feedBackItem = vm.$route.query.row;
next(vm.feedBackItem);
});
} else {
next();
}
},
beforeRouteUpdate(to, from, next) {
if(from.path == '/recovery-list') {
this.feedBackItem = this.$route.query.row;
}
next();
},
```
在以上代码中,我们在 `beforeRouteEnter` 钩子函数中判断是否是从 `/recovery-list` 路由跳转过来的,若是,则将 `feedBackItem` 数据属性赋值为路由参数的 `row` 值,并在调用 `next` 方法时将该值作为参数传递到回调函数中。在组件实例创建完成后,回调函数会被调用,此时我们可以访问到 `feedBackItem` 数据属性。
另外,为了防止在当前路由组件中切换路由时,`feedBackItem` 数据属性的值不更新,我们还需要在 `beforeRouteUpdate` 钩子函数中手动更新该值。