简体   繁体   English

如果我在另一个组件中添加beforeRouteEnter,则$ route在一个组件中变得不确定

[英]this.$route becomes undefined in one component if I add beforeRouteEnter in another

I'm stuck. 我被卡住了。 For some reason I can't access this.$route anymore in one component after I put beforeRouteEnter method in another component. 由于某种原因,在将RouteEnter方法放在另一个组件中之后,我无法再在一个组件中访问this。$ route。

My app looks like this. 我的应用看起来像这样。

<div id="app">
    <settings-modal></settings-modal>

    <router-view name="CalendarMonthView"></router-view>
</div>

The CalendarMonthView component is loaded in the router view. CalendarMonthView组件已加载到路由器视图中。 And there is a beforeRouteEnter inside it. 并且里面有一个beforeRouteEnter。 But then my SettingsModal component says that on created() the this.$params is undefined. 但是然后我的SettingsModal组件说在created()上this。$ params是未定义的。 Before I used beforeRouteEnter it wasn't undefined. 在使用beforeRouteEnter之前,它并不是未定义的。

CalendarMonthView component CalendarMonthView组件

export default {
    ...

    // to.params.selectedYear  = 2019
    // to.params.selectedMonth = 3

    beforeRouteEnter (to, from, next)
    {
        Month.getDays({
            year: to.params.selectedYear,
            month: to.params.selectedMonth,
         }).then(response => {
             store.monthDays = response.data
             next()
         })
    }
}

SettingsModal component SettingsModal组件

export default {
    ...

    created()
    {
        // undefined
        console.log('selectedYear: ', this.$route.params.selectedYear)

        // undefined
        console.log('selectedMonth: ', this.$route.params.selectedMonth)
    }
}

I don't know what you mean by this function Month.getDays() . 我不知道您所说的这个函数Month.getDays() Maybe there was an error in the function that caused next() not to be called.You should check that your function is correct. 函数中可能存在错误,导致未调用next()应检查函数是否正确。

Route params aren't available yet as the navigation hasn't finished. 由于导航尚未完成,因此路线参数尚不可用。 It needs to wait for Month.getDays promise to resolve. 它需要等待Month.getDays承诺解决。

You try to access the params from SettingsModal as soon as it is created which happens before the promise resolves. 您尝试从SettingsModal创建参数后立即访问这些参数,这会在Promise解决之前发生。

You can try to watch $route and check the params then. 您可以尝试watch $route并检查参数。

watch: {
  '$route': 'checkParams'
 },

This goes in SettingsModal . 这在SettingsModal

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM