路由优化大师
路由优化大师是一款及简单的路由器设置管理软件,其主要功能是一键设置优化路由、屏广告、防蹭网、路由器全面检测及高级设置等,有需要的小伙伴快来保存下载体验吧!
这篇文章主要介绍了浅谈vue-router 路由传参的方法,现在分享给大家,也给大家做个参考。
路由传参数。在很多时候我们需要路由上面传递参数,比如新闻列表页,我们需要传递新闻ID,给新闻详细页。
1.新闻列表页模板
<template> <p> </p> <h2>新闻列表</h2> <ul> <li> <router-link>新闻001</router-link> </li> <li> <router-link>新闻002</router-link> </li> </ul> </template>
我们访问/news/001,跳转到新闻详细页,展示001的这条新闻。
2.新闻详细页组件准备
<template> <p> 新闻详细页面 <span>{{$route.params.id}}</span> </p> </template>
$route.params.id获取路由上的参数
3.定义路由,增加一个路由
{ path: '/news/:id', component: NewsDetail },
4.全部代码如下:
nbsp;html> <title></title> <meta> <script></script> <script></script> <p> </p><p> <router-link>home</router-link> <router-link>news</router-link> </p> <router-view></router-view> <!-- 模板抽离出来 --> <template> <!-- 注意:组件只能有一个根元素,所以我们包装到这个p中 --> <p> </p> <h2>首页</h2> <router-link>登录</router-link> <router-link>注册</router-link> <!-- 路由匹配到的组件将渲染在这里 --> <router-view></router-view> </template> <template> <p> </p> <h2>新闻列表</h2> <ul> <li> <router-link>新闻001</router-link> </li> <li> <router-link>新闻002</router-link> </li> </ul> </template> <template> <p>登录界面</p> </template> <template> <p>注册界面</p> </template> <template> <p> 新闻详细页面 <span>{{$route.params.id}}</span> </p> </template> <script> // 1. 定义(路由)组件。 const Home = { template: '#home' }; const News = { template: '#news' }; const Login = { template: '#login' }; const Reg = { template: '#reg' }; //新闻详细页组件 const NewsDetail = { template: '#NewsDetail' }; // 2. 定义路由 const routes = [ { path: '/', redirect: '/home' }, { path: '/home', component: Home, children:[ { path: '/home/login', component: Login}, { path: '/home/reg', component: Reg} ] }, { path: '/news', component: News,}, { path: '/news/:id', component: NewsDetail }, ] // 3. 创建 router 实例,然后传 `routes` 配置 const router = new VueRouter({ routes // (缩写)相当于 routes: routes }) // 4. 创建和挂载根实例。 // 记得要通过 router 配置参数注入路由, // 从而让整个应用都有路由功能 const app = new Vue({ router }).$mount('#box') // 现在,应用已经启动了! </script>
上面是我整理给大家的,希望今后会对大家有帮助。
相关文章:
前端入门到VUE实战笔记:立即学习
>在学习笔记中,你将探索 前端 的入门与实战技巧!
已抢7337个
抢已抢95475个
抢已抢14940个
抢已抢52729个
抢已抢195932个
抢已抢87512个
抢