Backend routing: The URL request address corresponds to the resources on the server, and different resources are returned according to different request addresses. Front-end routing: In a single-page application, based on events triggered by the user, changing the URL changes the displayed content without refreshing the page. 1. Front-end routing implementation principleURL hash pattern When changing the Change the hash value of the page through We found that the page would not refresh. In
The three major front-end frameworks all have their own routing: 2. Basic use of vue-Router2.1. Installationnpm install vue-router --save The premise of using Import vue-router into index.js in the router folder import Vue from 'vue' import VueRouter from 'vue-router' Vue.use(VueRouter) //reference vue-router plugin 2.2. Configure routing//Configure the relationship between routing and components const route=[ { path: '/', // When accessing '/', the route redirects to the new address '/home' redirect: '/home', }, { path: '/home', component: home, }, { path: '/login', component: login, }, ] 2.3. Instantiationconst router = new VueRouter({ mode: "history", //Set mode routes }); 2.4. Mounting Routes Go to let app = new Vue({ el:'#app', data:{}, // Mount to the vue router, }) 2.5. Add router-link and router-view to the page<!-- Add a route --> <router-link to="/home">Home</router-link> <router-link to="/about">About</router-link> <!-- Display routing content--> <router-view /> By default, <router-link to="/home">Home</router-link> // Render as <a href="#/home" rel="external nofollow" >Homepage</a>
3. Router mode There are two routing modes: 3.1 Hash mode The anchor in 3.2 History Mode The required mode can be specified in const router = new VueRouter({ mode:'history' }) 4. Router-link properties By default, 4.1. Tag attribute The tag attribute is used to set the router-link tag rendering type. For example, if we want to render <router-link to="/home" tag="button">Home</router-link> Check the rendered element and you will find that it has become a In addition to 4.2. replace attribute replace corresponds to <router-link to="/home" replace>Home</router-link> 4.3, active-class <a href="#/" rel="external nofollow" aria-current="page" class="router-link-exact-active router-link-active"> Home page</a> To set the current element style, you need to set it to: Set active-class, such as: <router-link to="/" active-class="active">Home</router-link> // router-link-active class name will be replaced with active If you need to change the global const router = new VueRouter({ routes, mode: 'hash', linkActiveClasss: 'active' //Set class names uniformly}) 5. vue-Rrouter page jump method5.1. router-link implementation// Simple writing <router-link to="/">Home</router-link> //Use dynamic path <router-link :to="{ path : '/' }"></router-link> You can use path or name // Jump to 1 with parameter passing <router-link :to=" { name:'home', params:{ id:'123' , name:'gq' }} "> </router-link> // Jump with parameter <router-link :to=" { path:'/', query:{ id:'123' , name:'gq' }} "> </router-link> 5.2. Redirection via js// Simple writing this.$router.push({ path:'/' }) // push is the same as history.pushState // jump with parameters this.$router.push({ name: 'home' , params: { id:'123' , name:'gq' } }) //With multiple parameters this.$router.push({ name: 'home' , params: { id:'123' , name:'gq' }, query: { plan:'private' } }) 6. Dynamic Routing In some cases, the Configuring Routes routers:[ { path: '/user/:id', component:()=>{ import('../views/user.vue') } } ] Adding Routes <router-link to="/user/123"> user:123 </router-link> <router-link to="/user/456"> user:456 </router-link> //Dynamically set the id value <router-link :to=" '/user/'+id "> user:{{ id }} </router-link> Get the dynamic value behind this.$route.params.id The id here is the id set in the configuration route, just keep it consistent Method 2: Use query to pass parameters <router-link to="/user?id=123"></router-link> //When getting the value this.$route.query.id In addition, 7. Lazy loading of routesLazy loading means loading when it is used and not loading when it is not used. When packaging and building an application, the js package will become very large, affecting the loading speed. If we can split the components corresponding to different routes into different code blocks, and then load the corresponding components when accessing the route, it will be more efficient. What exactly does lazy loading of routes do? The main function is to package the components corresponding to the route into a js code block, and only load the corresponding js when the route is accessed. //Directly reference import Home from './component/home' const routes = [ { path:'/home', component:Home } ] //Lazy loading const routes = [ { path:'/home', component:()=>{ import('./component/home') } } ] 8. Nested RoutesIn practical applications, it is usually composed of multiple layers of nested components. Implementation steps: First: create the corresponding sub-component and configure the corresponding sub-route in the route mapping. Second: Use { path: "/body", component: ()=> import( "../components/bodyLayout.vue"),, children:[ { path: "manager", component: ()=> import( "../components/blankView.vue"), children:[ { path: "user", component: ()=> import( "../views/manager/user.vue") }, ] }, ] } When accessing the Note: When setting the path for nested routes, do not add "/", otherwise the route will change. { path: "/user", component: ()=> import( "../views/manager/user.vue") } //At this point the access route becomes "/user" 9. Difference between router and route Try to print We found that the two results were exactly the same. In this way, it is not difficult for us to understand the following meaning. route is the currently active route object, which contains the current route information. Through this object, you can get 10. Routing GuardWhy use navigation guards? Let's consider a requirement: In a SPA application, how does the webpage title change as the page switches? // Add created(){ in the corresponding component document.title="Test" } When you access this component, the title automatically switches to "Test" If we use the above method, we will have to add as many components as the ones we have developed, which is too troublesome. Therefore, we need to use routing guards to make unified modifications and facilitate maintenance. 10.1 Global Guard 1>, Use router.beforeEach((to,from,next)=>{ //Route from from to to // We just need to add a name attribute to the route document.title = from.name next() }) Note: The order of the above three parameters cannot be changed. next cannot be missing, it must be added, otherwise the page will not be able to jump to the next step and will be stuck in the blank area. 2>, Use router.afterEach((to,from)=>{ console.log('back guard') }) Both guards are global guards. 10.2. Exclusive router guardThe guards defined directly in the route configuration are used in the same way as global guards, except that they are placed in one of the route objects and only work under this route. { path: "/test", name: "Test", component: ()=> import( "../views/manager/test.vue"), beforeEnter:(to,from,next)=>{ console.log('before test enters') next() } } These guards have the same method arguments as the global preceding guards. 10.3. Guards within componentsYou can define route navigation guards directly in the route component. The guards defined in the component are in-component guards. const Foo = { template: `...`, beforeRouteEnter(to, from, next) { // Called before the corresponding route that renders this component is confirmed // No! able! Get the component instance `this` // Because the component instance has not been created before the guard is executed}, beforeRouteUpdate(to, from, next) { // Called when the current route changes but the component is reused // For example, for a path /foo/:id with dynamic parameters, when jumping between /foo/1 and /foo/2, // Since the same Foo component will be rendered, the component instance will be reused. And this hook will be called in this case. // You can access the component instance `this` }, beforeRouteLeave(to, from, next) { // Called when navigating away from the corresponding route of this component // You can access the component instance `this` } } Note: 11. keep-alive When switching routes, the page will be re-rendered every time. Some of our components will have some data that needs to be retained. We don’t want to re-render every time we switch back and forth, so we use <div id="app"> <router-link to="/home">home</router-link> <router-link to="/login">login</router-link> <keep-alive> <router-view></router-view> </keep-alive> </div> Add a 11.1. Keep-alive specific methodsactivated(){ console.log('activated') }, deactivated(){ console.log("deactivated") } These two functions will only be executed if the component is kept in state and Keep-alive life cycle execution order: 11.2. keep-alive attributeKeep-alive has two very important properties: <keep-alive include="test"> <router-view></router-view> </keep-alive> //test component adds name attribute <template> <div> test </div> </template> <script> export default { name:'test' } <script> At this time, only If there are other components that need to be cached at the same time, <keep-alive include="test,user"> <router-view></router-view> </keep-alive> Use of exclude: <keep-alive exclude="test,user"> <router-view></router-view> </keep-alive> Just the opposite of the previous example, only This is the end of this article about the detailed process of vue-Router installation principle. For more relevant vue-Router content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: How to get the height of MySQL innodb B+tree
>>: Tutorial on how to quickly deploy a Nebula Graph cluster using Docker swarm
How PHP works First, let's understand the rel...
Front-end is a tough job, not only because techno...
This article shares the specific code of jQuery t...
Install fastdfs on Docker Mount directory -v /e/f...
Write a SQL first SELECT DISTINCT from_id FROM co...
First, let me explain the application method. The...
Table of contents step 1. Configure routing rules...
After CentOS 7 is successfully installed, OpenJDK...
Table of contents Preface 1. The request content ...
background In order to support Docker containeriz...
This article example shares the specific code of ...
This article shares the specific code of Vue.js f...
Solution to forgetting MySQL password: [root@loca...
The garbled code problem is as follows: The reaso...
MySQL's foreign key constraint is used to est...