This time I will bring you the method of passing parameters between vue-router components. What are the precautions for passing parameters between vue-router components? The following is a practical case, let's take a look.
Use VueRouter to implement jumps between components: parameter transfer, the specific content is as follows login ---username--->main ①Clear the sender and receiver ②Configure the routing address of the receiver{path:'/myTest',component:TestComponent}
-->
{path:'/myTest/:id',component :TestComponent}
this.$route.params.id
this.$router.push('/myTest/20')
nbsp;html> <meta> <title>传参</title> <script></script> <script></script> <p> </p><p>{{msg}}</p> <!--指定容器 --> <router-view></router-view> <script> //创建主页面组件 var myMain = Vue.component("main-component",{ //保存登录传递过来的数据 data:function(){ return { uName:'' } }, template:` <p> <h1>主页面用户名:{{uName}} `, //挂载该组件时自动拿到数据 beforeMount:function(){ //接收参数 console.log(this.$route.params); this.uName = this.$route.params.myName ; } }) //创建登录页面组件 var myLogin = Vue.component("login-component",{ //保存用户输入的数据 data:function(){ return { userInput:"" } }, methods:{ toMain:function(){ //跳转到主页面,并将用户输入的名字发送过去 this.$router.push("/main/"+this.userInput); console.log(this.userInput); } }, template:` <p> <h1>登录页面 <input type="text" v-model="userInput" placeholder="请输入用户名"> <button @click="toMain">登录到主页面 <br> <router-link :to="'/main/'+userInput">登录到主页面 ` }) var NotFound = Vue.component("not-found",{ template:` <p> <h1>404 Page Not Found <router-link to="/login">返回登录页 ` }) //配置路由词典 const myRoutes = [ {path:"",component:myLogin}, {path:"/login",component:myLogin}, //注意冒号,不用/否则会当成地址 {path:"/main/:myName",component:myMain}, //没有匹配到任何页面则跳转到notfound页面 {path:"*",component:NotFound} ] const myRouter = new VueRouter({ routes:myRoutes }) new Vue({ router:myRouter, el:"#container", data:{ msg:"Hello VueJs" } }) // 注意,路由地址 </script>
nbsp;html> <meta> <title>传参练习</title> <script></script> <script></script> <p> </p><p>{{msg}}</p> <!-- --> <router-view></router-view> <script> //创建产品列表组件 var myList = Vue.component("product-list",{ //保存产品列表的数据 data:function(){ return{ productList:["苹果","华为","三星","小米","vivo"] } }, template:` <p> <h4>这是列表页 <ul> <li v-for="(tmp,index) in productList"> //将index传递过去 <router-link v-bind:to="'/detail/'+index">{{tmp}} ` }) //详情页组件 var myDetail = Vue.component("product-detail",{ //保存传递过来的index data:function(){ return{ myIndex:"" } }, //在挂载完成后,将接收到的index赋值给myIndex mounted:function(){ this.myIndex = this.$route.params.id; }, template:` <p> <h4>这是详情页 <p>这是id为:{{myIndex}}的产品 ` }) //页面找不到的时候 var NotFound = Vue.component("not-found",{ template:` <p> <h1>404 Page Not Found ` }) // 配置路由词典 const myRoutes = [ {path:"",component:myList}, {path:"/list",component:myList}, {path:"/detail/:id",component:myDetail}, {path:"*",component:NotFound}, ] const myRouter = new VueRouter({ routes:myRoutes }) new Vue({ router:myRouter, el:"#container", data:{ msg:"Hello VueJs" } }) </script>I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website! Recommended reading:
mint-ui loadmore Pull-up loading and pull-down refresh conflict handling method
How about the WeChat applet Make the image upload to the server
The above is the detailed content of How to pass parameters between vue-router components. For more information, please follow other related articles on the PHP Chinese website!

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe


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

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version
Recommended: Win version, supports code prompts!
