This article mainly introduces the usage of Vue-Router mode and hooks. Now I will share it with you and give you a reference.
The previous article mainly wrote about the basic use of vuer-router. It can be said that it solves the problem of food and clothing. Now let’s have some afternoon tea
Mode
The mode options in vue-router are mainly defined when the router is instantiated, as follows
const router = new VueRouter({ mode: 'history', // 两种类型history 还有 hash routes: routes // 可以缩写成routes })
There are two modes to choose from, history and hash, for a rough comparison,
Mode | Advantages | Disadvantages |
---|---|---|
Use Simple, no need for background support | Exists in the form of hash in the URL and will not be transmitted to the background | |
The address is clear and easy to understand and the background Processing | requires background cooperation |
The final routing of the history mode is reflected in the pathname of the URL. This part will be transmitted to the server, so the server needs to map each possible path value accordingly. Or use fuzzy matching for mapping.
404 route definition
Since the router itself is matched from top to bottom, if a matching route is found earlier, it will jump. Therefore, you can directly add the 404 route at the end, as followslet routerConfig = [{ path: '/pages', component: App, children: [{ path: 'demo/step1/list', component: coupon, name: 'coupon-list', meta: { title: '红包' } }] }, { path: '*', component: NotFound, name: 'notfound', meta: { title: '404-页面丢了' } }]
Routing hook
Routing hooks are mainly defined for users to perform some special processing when routing changes. Damn. . What a mouthful.2, hooks exclusive to a certain route
3, In-component hooks
Global hooks
As the name suggests, global hooks are used globally and are used as followsconst router = new VueRouter({ mode: 'history', base: __dirname, routes: routerConfig }) router.beforeEach((to, from, next) => { document.title = to.meta.title || 'demo' if (!to.query.url && from.query.url) { to.query.url = from.query.url } next() }) router.afterEach(route => { })
Exclusive hooks for a certain route
As mentioned, it is used separately for a certain route, which is essentially the same as the hook in the subsequent component. They all refer to a specific route. The difference is that the general definition here is in the router, not in the component. As followsconst router = new VueRouter({ routes: [ { path: '/foo', component: Foo, beforeEnter: (to, from, next) => { // ... }, beforeLeave: (to, from, next) => { // ... } } ] })
Hooks within the component
First look at the official definition:You can directly define the following routing navigation hooks within the routing component
beforeRouteLeave(to, from, next) { // .... next() }, beforeRouteEnter(to, from, next) { // .... next() }, beforeRouteUpdate(to, from, next) { // .... next() }, computed: {}, method: {}
How to replace node elements with jQuery
Servlet3.0 interacts with pure javascript through Ajax Detailed example explanation
Instance code of tree-shaped dish implemented by vue
The above is the detailed content of How to use Vue-Router patterns and hooks (detailed tutorial). For more information, please follow other related articles on the PHP Chinese website!