如何在单文件组件中正确使用 Vue Router beforeRouteEnter 或 Watch 触发方法?

How to Properly Use Vue Router beforeRouteEnter or Watch to trigger method in Single File Component?(如何在单文件组件中正确使用 Vue Router beforeRouteEnter 或 Watch 触发方法?)
本文介绍了如何在单文件组件中正确使用 Vue Router beforeRouteEnter 或 Watch 触发方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I'm working on an app in Vue.js using Single File Components and Vue Router. I have a Search component where I need to execute a method to re-populate search results each time a user visits the route. The method executes correctly the first time the route is visited because of the "create" hook:

created: function() {
    this.initializeSearch();
  },

However, when the user leaves the route (to register or log into the app for instance), and returns to the Search page, I can't seem to find a way to automatically trigger this.initializeSearch() on subsequent visits.

Routes are set up in index.js like so:

import Search from './components/Search.vue';
import Login from './components/Login.vue';
import Register from './components/Register.vue';

// Vue Router Setup
Vue.use(VueRouter)

const routes = [
  { path: '/', component: Search },
  { path: '/register', component: Register },
  { path: '/login', component: Login },
  { path: '*', redirect: '/' }
]

export const router = new VueRouter({
  routes
})

I gather that I should be using "watch" or "beforeRouteEnter" but I can't seem to get either to work.

I tried using "watch" like so within my Search component:

watch: {
    // Call the method again if the route changes
    '$route': 'initializeSearch'
  }

And I can't seem to find any documentation explaining how to properly use the beforeRouteEnter callback with a single file component (the vue-router documentation isn't very clear).

Any help on this would be much appreciated.

解决方案

Since you want to re-populate search results each time a user visits the route.

You can use beforeRouteEnter() in your component as below:

beforeRouteEnter (to, from, next) { 
  next(vm => { 
    // access to component's instance using `vm` .
    // this is done because this navigation guard is called before the component is created.            
    // clear your previously populated search results.            
    // re-populate search results
    vm.initializeSearch();
  }) 
} 

You can read more about navigation guards here

Here is the working fiddle

这篇关于如何在单文件组件中正确使用 Vue Router beforeRouteEnter 或 Watch 触发方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

initialising a global mixin in weex我使用 weex create awesome-project 初始化了一个 weex 和 vue 应用程序。在 entry.js 文件中,我试图注册一个全局 mix...
Correctly setup Eslint Airbnb with VScode in Vue project这是我没有 linting 的 Vuejs 代码。在我运行 之后 npm run lint --fix代码是这样的但我再次做...
TypeError: Cannot read property 'get' of undefined, Axios, Vue.JS尝试使用 axios 从 api 访问获取请求时,我从 Vue 收到这个奇怪的错误,我收到 TypeErro...
How to call a vue.js function on page load我有一个帮助过滤数据的功能。当用户更改选择时,我正在使用 v-on:change 但我还需要在用户选择数据之前调用该函...
How can I call method in the component from view? (vue.js 2)我的看法是这样的:[cc lang=javascript] England ...
Passing Data between react components(在Reaction组件之间传递数据)