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!

Vue应用中遇到vue-router的错误“NavigationDuplicated:Avoidedredundantnavigationtocurrentlocation”–怎么解决?Vue.js作为快速而灵活的JavaScript框架在前端应用开发中越来越受欢迎。VueRouter是Vue.js的一个代码库,用于进行路由管理。然而,有时

在Vue应用中使用vue-router时,有时候会出现“Error:Avoidedredundantnavigationtocurrentlocation”的错误信息。这个错误信息的意思是“避免了到当前位置的冗余导航”,通常是因为重复点击了同一个链接或者使用了相同的路由路径导致的。那么,怎么解决这个问题呢?使用exact修饰符在定义router

Vue-Router:如何使用路由元信息来管理路由?简介:Vue-Router是Vue.js官方的路由管理器,它可以帮助我们快速构建单页应用程序(SPA)。除了常见的路由功能外,Vue-Router还支持使用路由元信息来管理和控制路由。路由元信息是可以附加到路由上的自定义属性,它可以帮助我们实现一些特殊的逻辑或者权限控制。一、什么是路由元信息?路由元信息是

在Vue应用中使用vue-router是一种常见的方式来实现路由控制。然而,在使用vue-router的时候,有时候会出现“Error:Failedtoresolveasynccomponent:xxx”的错误,这是由于异步组件加载错误导致的。在本文中,我们将探讨这个问题,并提供解决方案。理解异步组件加载原理在Vue中,组件可以被同步或异步地创建

Vue是一个流行的前端框架,它允许开发者快速构建高效、可重用的web应用程序。Vue-router是Vue框架中的一个插件,可以帮助开发者轻松管理应用的路由和导航。但是,在使用Vue-router的过程中,有时候会遇到一个常见的错误:“Error:Invalidroutecomponent:xxx”。这篇文章将介绍这个错误的原因和解决方法。原因在Vu

Go语言中的context包是用来在程序中传递请求的上下文信息的,它可以在跨多个Goroutine的函数之间传递参数、截取请求和取消操作。在Go中使用context包,我们首先需要导入"context"包。下面是一个示例,演示了如何使用context包实现请求参数传递。packagemainimport("context"

Vue和Vue-Router:如何在组件之间共享数据?简介:Vue是一个流行的JavaScript框架,用于构建用户界面。Vue-Router是Vue的官方路由管理器,用于实现单页面应用。在Vue应用中,组件是构建用户界面的基本单位。在许多情况下,我们需要在不同的组件之间共享数据。本文将介绍一些方法,帮助你在Vue和Vue-Router中实现数据共享,以及


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version
Visual web development tools

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 Linux new version
SublimeText3 Linux latest version

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function