search
HomeWeb Front-endJS Tutorialvue-router project practice (detailed tutorial)

vue-router project practice (detailed tutorial)

Jun 06, 2018 am 10:15 AM
routervueProject actual combat

vue-router is the official routing library of Vue.js. This article mainly introduces the practical summary of the vue-router project. Friends who need it can refer to it

Let’s talk about the vue project today{ vue,vue-router,component} vue-router, one of the three great generals. As one of our important practices for front-end and back-end separation, router helps us complete page jumps between SPA applications.

And, with third-party libraries like axios, we can implement interceptor functions that work with the background interface.

For a small project, it is enough that the router folder contains a router.js.

However, when we have a lot of pages, we need to divide them into Come out two files: one defines our routes and components, and the other instantiates the components and mounts the route to the vue instance.

I won’t go into details about the basic usage. You can check out the official website of vue-router. If you read it carefully, there will definitely be no problem with basic usage.

1. Why does my routing not work?

A very important point here is when we construct an instance of VueRouter Sometimes, there is a problem with the parameters passed in.

import routes from '@/router/router'
const router = new VueRouter({
 routes // (ES6语法)相当于 routes: routes
})
new Vue({
 router
}).$mount('#app')

If what you introduce here is not routes, you have to write it in the following way.

import vRoutes from '@/router/router'
const router = new VueRouter({
 routes :vRoutes 
})
new Vue({
 router
}).$mount('#app')

2. Implement lazy loading of components based on webpack in routing

For our vue projects, we basically use webpack for packaging Yes, if there is no lazy loading, the packaged files will be abnormally large, resulting in a white screen on the homepage and serious delays, which is not conducive to the user experience. Lazy loading can be used to divide the page, and webpack packages different components into many Small js file. Load asynchronously when needed to optimize the user experience. In other words, only one or two of 100 users may enter some pages, so why spend traffic on it.

import App from '@/App.vue'
const index = r => require.ensure([], () => r(require('@/pages/index/index')), 'index')
export default [{
 path: '/',
 component: App,
 children: [
  {
    path: '/index',
    name:'index',
    component: index
  }]
}]

If a component contains nested routes, we can also package the two routes into a js chunk.

// 这两条路由被打包在相同的块中,访问任一路由都会延迟加载该路由组件
const orderUser= r => require.ensure([], () => r(require('@/pages/order/user')), 'order')
const payRecord= r => require.ensure([], () => r(require('@/pages/order/payRecord')), 'order')

3.Router mode

For browsers, our router is divided into two modes.

1.hash mode (default)

According to the basic structure of a uri, the hash mode is processed in a basic URI fragment. If we put aside SPA, the more common application scenario is that when we are building a PC mall, there will be tab switching such as product details, comments, and product parameters. We can use the a tag with the ID, and add a little movement. The special effects are very good.

This is also the routing method used by router by default. However, this method has a drawback, that is, when connecting to a third-party payment, we pass in a URL to the third-party payment as a callback address, but after the payment is completed, some third-party payment will transfer our # As an interception symbol, only the url content before the first # symbol is retained, and the corresponding callback parameters are added later. As a result, after the payment is completed, it cannot jump to the corresponding payment page

Incoming url:

http://xx.xx.com/#/pay/123

Address after callback:

http://xx.xx.com/pay/123?data=xxxxx%xxxx

2.history mode

There is also a history mode. It uses h5's history.pushState to complete the URL jump. The advantage of using this method to handle jumps is that the URL is no different from what we usually see. If compared with the hash mode, there is no #. However, when using history mode, we also need to do corresponding processing in the background, because if we directly access an address, such as http://www.xxxx.com/user/id, if the backend is not configured, then The client will return a 404 page.

4.router-link is in the loop this.Parameter name=undefined

##The component is our view layer The jump component that needs to be used. It replaces what the tag needs to do, and helps us do a lot more.

Whether it is h5 history mode or hash mode, its performance behavior is consistent, so when you want to switch routing mode, or downgrade to use hash mode in IE9, no changes are required.

In HTML5 history mode, router-link will guard the click event so that the browser will not reload the page.

When you use the base option in HTML5 history mode, all to attributes do not need to be written (base path).

But when we use router-link in a v-for loop, generally speaking, what we need to get are the values ​​in the loop, which can be obtained through the defined item.xxx. If we need to get a value we defined in data, do we get it through this.foo? Or should I retrieve it through foo? Or is it okay?

Here, we cannot get it through this.foo, because this here no longer points to the instance of vue, but points to [object Window]. So if you use this.foo to retrieve it, it is actually undefined.

 <router-link tag="li" :to="{path:`/user/${item.userID}`}" v-for="(item, index) in userList" :key="index">
   //含有固定的值
  <p>{{this.foo}}</p>
  <p>{{foo}}</p>
 </router-link>
data(){
  return {
    foo:&#39;bar&#39;,
  } 
}

4. Using vue-router with axios

 初次接触拦截器这个概念是在java中,通过拦截器,我们可以对用户的登录状态进行更加粒度的操作。而对于一个SPA的应用来说,没有了后台路由的介入,我们就需要在前端实现一套自己的登录状态的管理机制。

最直观的一点就是,通过用户的token来判断用户是否登录?

router.beforeEach((to, from, next) => {
 const NOW = new Date().getTime();
 if (to.matched.some(r => r.meta.requireAuth)) {
  if(NOW > store.state.deadLine){
   store.commit(&#39;CLEAR_USERTOKEN&#39;)
  }
  if (store.state.message.login === true) {
   next();
  }
  else {
   next({
    path: &#39;/login&#39;,
    query: {redirect: to.fullPath}
   })
  }
 }
 else {
  next();
 }
})

上面的代码中,我们通过vue-router中的全局守卫,在导航触发的时候大致做了如下几件事:

(1)判断导航的页面是否需要登录

(2)超过登录持久期限,清除持久化的登录用户token

(3)没有超过登录期限,判断是否登录状态

(4)没登录,重定向到登录页面

但是,仅仅这样是不够的。因为用户直接不正常注销而直接后台运行网页是很正常的事情,这就导致虽然token是存在的,但是对于后台而言,这个token是无效的,过期的了。所以,我们需要axios配合后台给出的状态码来完善我们的拦截器。

import router from &#39;@/router/routes&#39;
axios.interceptors.response.use(
 success => {
  switch (success .code) {
   case -100:
    router.replace({
     path: &#39;login&#39;,
     query: {redirect: router.currentRoute.fullPath}
    })
    console.warn(&#39;注意,登录已过期!&#39;)
    break;
  }
  return success;
 },
 error => {
   switch (error.code) {
    case 404:
     console.warn(&#39;请求地址有误或者参数错误!&#39;)
    break;
   }
  return Promise.reject(error.response.data)
 });

 通过后端给到的登录过期状态码,这里以-100为例,我们可以用axios的响应拦截器实现,当我们的token过期的时候,我们将页面重定向到登录页面去。

 5.巧用replace替换push

在项目中,我有的同事就是一直this.$router.push(...),从开始push到结尾。

碰到有的页面,比如说,在选择地址的时候需要知道用户当前所在的城市,如果没有的话,就是重定向到城市列表页面去手动选取。选择完成以后再回到选择地址的页面,如果一直使用push的话,点击选择地址的后退时,就会回退到城市列表页。然后造成页面间的死循环。

这里如果使用replace来操作就没有什么问题了,问题就是我们不应该让城市列表页出现在我们的浏览历史里面。

上面是我整理给大家的,希望今后会对大家有帮助。

相关文章:

在javascript中如何实现填充默认头像

JavaScript的6种正则表达式(详细教程)

react webpack打包后的文件(详细教程)

The above is the detailed content of vue-router project practice (detailed tutorial). For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
JavaScript in Action: Real-World Examples and ProjectsJavaScript in Action: Real-World Examples and ProjectsApr 19, 2025 am 12:13 AM

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

JavaScript and the Web: Core Functionality and Use CasesJavaScript and the Web: Core Functionality and Use CasesApr 18, 2025 am 12:19 AM

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding the JavaScript Engine: Implementation DetailsUnderstanding the JavaScript Engine: Implementation DetailsApr 17, 2025 am 12:05 AM

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python vs. JavaScript: The Learning Curve and Ease of UsePython vs. JavaScript: The Learning Curve and Ease of UseApr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python vs. JavaScript: Community, Libraries, and ResourcesPython vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AM

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

From C/C   to JavaScript: How It All WorksFrom C/C to JavaScript: How It All WorksApr 14, 2025 am 12:05 AM

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

JavaScript Engines: Comparing ImplementationsJavaScript Engines: Comparing ImplementationsApr 13, 2025 am 12:05 AM

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Beyond the Browser: JavaScript in the Real WorldBeyond the Browser: JavaScript in the Real WorldApr 12, 2025 am 12:06 AM

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)