Vue.js 当用户在Vue和Vuex中从第一条路线转到第二条路线以及从第二条路线转到第一条路线时,是否希望在第一条路线上运行功能
我想在加载的路径(第一条路径)上运行一个函数,然后转到第二条路径。用户再次从第二条路线转到第一条路线 当用户在Vue和Vuex中从第二个路由转到第一个路由时,我想在第一个路由上运行一个函数 我尝试了Vue生命周期挂钩,如Created、beforeMount,但它们都不起作用 请告诉我我需要运行什么,以及如何在Vue项目上运行 谢谢vue路由器,您可以定义全局挂钩来调用路由器更改或 例如,一个示例:Vue.js 当用户在Vue和Vuex中从第一条路线转到第二条路线以及从第二条路线转到第一条路线时,是否希望在第一条路线上运行功能,vue.js,vuejs2,vuex,vue-router,Vue.js,Vuejs2,Vuex,Vue Router,我想在加载的路径(第一条路径)上运行一个函数,然后转到第二条路径。用户再次从第二条路线转到第一条路线 当用户在Vue和Vuex中从第二个路由转到第一个路由时,我想在第一个路由上运行一个函数 我尝试了Vue生命周期挂钩,如Created、beforeMount,但它们都不起作用 请告诉我我需要运行什么,以及如何在Vue项目上运行 谢谢vue路由器,您可以定义全局挂钩来调用路由器更改或 例如,一个示例: //router.js const router = new VueRouter({ ro
//router.js
const router = new VueRouter({
routes: [
{
path: '/night',
component: nightComponent,
beforeEnter: (to, from, next) => {
const currentHour = new Date().getHours();
if(currentHour < 6){
next(); // this function will trigger the routing. in my example, i
//call it only if its not morning yet.
}
}
}
]
})
new Vue({
el: "#app",
router,
data: {},
methods: {},
beforeRouteLeave (to, from, next) {
// called when the route that renders this component is about to
// be navigated away from.
// has access to `this` component instance.
if(confirm('are you sure you want to leave this page?')){
next();
}
}
}
})