This article mainly introduces the use of vue-router to complete simple navigation functions. It has certain reference value. Now I share it with you. Friends in need can refer to it.
vue-router is Vue. A set of dedicated routing tool libraries officially provided by js. This article mainly introduces the use of vue-router to complete simple navigation functions. Friends who need it can refer to it
vue-router is a dedicated routing tool library officially provided by Vue.js
The installation command is as follows
npm i vue-router -D
vue-router instance is a Vue plug-in, we need to connect it to Vue through Vue.use() in the Vue global reference In the instance.
In our project, main.js is the default program entry file, and all global configurations will be performed in this file.
We add the following reference to main.js
import VueRouter from 'vue-router' Vue.use(VueRouter)
This completes the most basic installation work of vue-router.
The functions we want to implement next are described as follows
There are two links on the homepage: shopping cart and personal center
Click on different links to display Different contents
First we create two component files in the src directory: Cart.vue Me.vue
The contents of the two newly created component files have the same structure for the time being.
<template> <!-- 这个p里面的内容可设置不同以区分 --> <p>购物车</p> </template> <script> export default {} </script> <style lang="scss"></style>
The next step is to define the matching rules between routing and these components in the main.js file.
The definition of VueRouter is very simple: create a VueRouter instance and specify the routing path to a component type
As shown in the following code (main.js)
import Vue from 'vue' import App from './App.vue' import VueRouter from 'vue-router' //引入创建的两个组件 import Cart from './Cart.vue' import Me from './Me.vue' //使用路由实例插件 Vue.use(VueRouter) const router = new VueRouter({ mode:'history', base: '__dirname', routes:[ //将页面组件与path指令的路由关联 {path:'/cart',component:Cart}, {path:'/me',component:Me} ] }) new Vue({ el: '#app', //将路由实例添加到Vue实例中去 router, render: h => h(App) })
We can extract the above routing-related code and put it in another routes.js file to prevent the content of the main.js file from getting longer.
Create a new config folder and add the routes.js file into it.
The routes.js code is as follows
import Vue from 'vue' import VueRouter from 'vue-router' //引入创建的两个组件 import Cart from '../Cart.vue' import Me from '../Me.vue' //使用路由实例插件 Vue.use(VueRouter) const router = new VueRouter({ mode:'history', base: '__dirname', routes:[ //将页面组件与path指令的路由关联 {path:'/cart',component:Cart}, {path:'/me',component:Me} ] }) export default router;
Then the main.js file code is reduced to the following:
import Vue from 'vue' import App from './App.vue' import router from './config/routes' new Vue({ el: '#app', //将路由实例添加到Vue实例中去 router, render: h => h(App) })
vue-router provides two instruction tags
: render the view component matched by the path : Supports users to navigate in applications with routing functions
With the two command tags above, we can write the corresponding code in the program entrance App.vue:
<template> <p id="app"> <p class="tabs"> <ul> <li> <router-link to ="/cart"> <p>购物车</p> </router-link> </li> <li> <router-link to ="/me"> <p>个人中心</p> </router-link> </li> </ul> </p> <p class="content"> <!-- 使用 router-view 渲染视图 --> <router-view></router-view> </p> </p> </template> <script> export default { name: "app" }; </script> <style lang="scss"></style>
The above code has achieved the expected function.
Then we see that the path in to ="/cart" has actually been defined in {path:'/cart',component:Cart}
. If you need to modify it, you have to It is necessary to modify these two places at the same time (if it is used in other places, change more)
Then directly change the path in {path:'/cart',component:Cart}
Wouldn’t it be nice to take it out.
At this time, our vue-router provides an implicit route reference method, called - named route
Simply put, it replaces Url# by the name reference of the route.
##So the configuration code of VueRouter was changed to the following:const router = new VueRouter({ mode:'history', base: '__dirname', routes:[ //将页面组件与path指令的路由关联 {name:'cart',path:'/cart',component:Cart}, {name:'me',path:'/me',component:Me} ] })In this way we are
The to attribute is bound to the Vue instance using v-bind, and then the Url is obtained directly through the name.
<li> <router-link :to ="{name:'cart'}"> <p>购物车</p> </router-link> </li> <li> <router-link :to ="{name:'me'}"> <p>个人中心</p> </router-link> </li>At this point, a simple navigation function has been completed using vue-routerInstructions
<li> <router-link :to ="{name:'cart'}" tag="span"> <p>购物车</p> </router-link> </li>The above is the entire content of this article, I hope it will be helpful Everyone’s learning is helpful. For more related content, please pay attention to the PHP Chinese website! Related recommendations:
About the form item verification method in the v-for loop when vue uses the Element component
Vue implements the idea of fixing the number of input lines in textarea and adding underline style
The above is the detailed content of Use vue-router to complete simple navigation functions. For more information, please follow other related articles on the PHP Chinese website!

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.

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 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 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 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.

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.

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.

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.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version
God-level code editing software (SublimeText3)