使用路由对象$route获取参数:

1、params:

参数获取:使用$route.params获取参数;

参数传递: URL传参:例 <route-linke to : "/foods/bjc/北京烤鸭/68">  注:在对应路由path上使用 /:+属性名称接收参数

实例:

需要在子组件的路由中定义所需的属性名;

代码:

<template id="foods">

        <div>

            <h2>美食广场</h2>
<ul>
<router-link to="/foods/bjc/北京烤鸭/68" tag="li"> 北京菜</router-link>
<router-link to="/foods/hnc" tag="li"> 湖南菜</router-link>
<router-link to="/foods/xc" tag="li"> 湘菜</router-link>
<router-link to="/foods/yc" tag="li"> 粤菜</router-link>
<router-link to="/foods/scc" tag="li"> 四川菜</router-link>
</ul> <router-view></router-view>
</div>
</template>
//定义foods中的子组件

        let Bjc={
template : "<h3>北京菜 菜名:{{$route.params.name}} 价格:{{$route.params.price}}</h3>" }
//2 配置路由 路由可能有多个
const myRoutes = [
{
path : "/home",
component : Home
},
{
path : "/foods",
component : Foods, children:[
{
path:"bjc/:name/:price",//定义其属性
component:Bjc },

对象传参:例 <route-linke :to : "xxObj">  注:对象格式{String name, Objce param} name是路由名称,param是传递参数的对象

对象需要通过v-bind进行绑定

<router-link :to="sccParam" tag="li"> 四川菜</router-link>

使用对象:

let Foods = {
template : "#foods",
data(){ return{
sccParam:{ name:'sccRouter', params:{ name:"麻婆豆腐",
price:
}
}
}
}
}
let Scc={
template : "<h3>四川菜 菜名:{{$route.params.name}} 价格:{{$route.params.price}}</h3>" }

效果:

注意:对象的名称必须和路由的名称保持一致,在路由中起的名称可以称作为命名路由

2、query:

参数获取:使用$route.query获取参数;

参数传递: URL传参:例 <route-linke to : "/foods/bjc?name=北京烤鸭&price=68">

代码:

    <router-link to="/foods/xc?name=剁椒鱼头&price=128" tag="li"> 湘菜</router-link>
let Xc={
template : "<h3 >湘菜 菜名:{{$route.query.name}} 价格:{{$route.query.price}}</h3>" }

对象传参:例 <route-linke :to : "xxObj">  注:对象格式{String path, Objce query} path是路由url,query是传递参数的对象

<router-link :to="ycParam" tag="li"> 粤菜</router-link>
let Foods = {
template : "#foods",
data(){ return{
sccParam:{ name:'sccRouter', params:{ name:"麻婆豆腐",
price:
}
}, ycParam:{
path:'/foods/yc',
query:{
name:"蜜汁叉烧",
price: } }
}
}
}
let Yc={
template : "<h3>粤菜 菜名:{{$route.query.name}} 价格:{{$route.query.price}}</h3>" }

以上实例的所有代码:

 <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>使用路由对象获取参数</title>
</head>
<body>
<div id="one">
<router-link to="/home">首页</router-link>
<router-link to="/foods">美食</router-link> <div>
<!--将数据显示在这里-->
<router-view></router-view>
</div>
</div>
</body>
<template id="foods"> <div> <h2>美食广场</h2>
<ul>
<router-link to="/foods/bjc/北京烤鸭/68" tag="li"> 北京菜</router-link>
<router-link to="/foods/hnc" tag="li"> 湖南菜</router-link>
<router-link to="/foods/xc?name=剁椒鱼头&price=128" tag="li"> 湘菜</router-link>
<router-link :to="ycParam" tag="li"> 粤菜</router-link>
<router-link :to="sccParam" tag="li"> 四川菜</router-link>
</ul> <router-view></router-view>
</div>
</template> <script type="text/javascript" src="../js/vue.js" ></script>
<script type="text/javascript" src="../js/vue-router.js" ></script>
<script> //1 定义组件
let Home = {
template : "<h2>首页</h2>"
}
let Foods = {
template : "#foods",
data(){ return{
sccParam:{ name:'sccRouter', params:{ name:"麻婆豆腐",
price:
}
}, ycParam:{
path:'/foods/yc',
query:{
name:"蜜汁叉烧",
price: } }
}
}
} //定义foods中的子组件 let Bjc={
template : "<h3>北京菜 菜名:{{$route.params.name}} 价格:{{$route.params.price}}</h3>" } let Hnc={
template : "<h3>湖南菜 </h3>" }
let Xc={
template : "<h3 >湘菜 菜名:{{$route.query.name}} 价格:{{$route.query.price}}</h3>" } let Yc={
template : "<h3>粤菜 菜名:{{$route.query.name}} 价格:{{$route.query.price}}</h3>" } let Scc={
template : "<h3>四川菜 菜名:{{$route.params.name}} 价格:{{$route.params.price}}</h3>" } //2 配置路由 路由可能有多个
const myRoutes = [
{
path : "/home",
component : Home
},
{
path : "/foods",
component : Foods, children:[
{
path:"bjc/:name/:price",//定义其属性
component:Bjc },
{
path:"hnc",
component:Hnc }, {
path:"xc",
component:Xc },
{
path:"yc",
component:Yc },
{
name:'sccRouter',
path:"scc",
component:Scc } ]
},
{
path:"*",
redirect:"/home"
}
] // 3 创建路由对象
const myRouter = new VueRouter({
//routes : routes
routes : myRoutes,
//mode:'history'
linkActiveClass : "active" }); new Vue({
//router : router
router : myRouter //4 注入路由 简写
}).$mount("#one");
</script>
<style> .active{
color: white; background-color: black;
}
</style>
</html>

使用路由对象获取参数

最后从上面的效果图中我们可以看到四川菜刷新后价格与菜名都消失了,所以使用params的对象传参的时候刷新的时候数据是获取不了的。

Vue-Router路由 Vue-CLI脚手架和模块化开发 之 使用路由对象获取参数的更多相关文章

  1. Vue-Router路由Vue-CLI脚手架和模块化开发 之 vue-router路由

    vue-router路由:Vue.js官网推出的路由管理器,方便的构建单页应用: 单页应用(SPA)只有一个web页面的应用,用户与应用交互时,动态更新该页面的内容:简单来说,根据不同的url与数据, ...

  2. [Vue 牛刀小试]:第十二章 - 使用 Vue Router 实现 Vue 中的前端路由控制

    一.前言 前端路由是什么?如果你之前从事的是后端的工作,或者虽然有接触前端,但是并没有使用到单页面应用的话,这个概念对你来说还是会很陌生的.那么,为什么会在单页面应用中存在这么一个概念,以及,前端路由 ...

  3. Vue-Router路由 Vue-CLI脚手架和模块化开发 之 路由常用配置与路由嵌套

    vue-router路由常用配置 1.mode:配置路由模式,默认为hash,由于URL很丑,可以修改为history,但是需要服务端的支持: 以上一篇的博文为实例: 初始时url的显示: 使用mod ...

  4. Vue-Router路由Vue-CLI脚手架和模块化开发 之 使用props替代路由对象的方式获取参数

    在上一章博文中使用路由对象$route获取参数时,组件和路由对象耦合,在这篇博文中就可以使用props来进行解耦: 1.在组件中使用props选项定义数据,接收参数: 2.在路由中,使用props选项 ...

  5. Vue-Router路由Vue-CLI脚手架和模块化开发 之 路由的动态跳转

    在上一篇的博文中,实现的跳转是在页面中进行实现的 利用vue-router实例方法,使用js对路由进行动态跳转: 1.router.push:参数为路由对象,跳转到指定路由,跳转后会产生历史记录: & ...

  6. vue router 中,children 中 path 为空字符串的路由,是默认打开的路由(包括在 el-tabs 中嵌套路由的情况)

    详见该页面的最后一个代码块:https://router.vuejs.org/zh/guide/essentials/nested-routes.html#%E5%B5%8C%E5%A5%97%E8% ...

  7. Vue.js 2.x笔记:路由Vue Router(6)

    1. Vue Router简介与安装 1.1 Vue Router简介 Vue Router 是 Vue.js 官方的路由管理器.它和 Vue.js 的核心深度集成,构建单页面应用. Vue Rout ...

  8. Vue系列:Vue Router 路由梳理

    Vue Router 是 Vue.js 官方的路由管理器.它和 Vue.js 的核心深度集成,让构建单页面应用变得易如反掌.包含的功能有: 嵌套的路由/视图表 模块化的.基于组件的路由配置 路由参数. ...

  9. Vue Router路由管理器介绍

    参考博客:https://www.cnblogs.com/avon/p/5943008.html 安装介绍:Vue Router 版本说明 对于 TypeScript 用户来说,vue-router@ ...

随机推荐

  1. python pip 安装模块步骤

    在线安装,cmd输入 命令:pip install mysql-connector-python 注:直接在默认路径下输入

  2. js 重写alert 兼容iphone使得alert 不带src

    <script> window.alert = function(name){ var iframe = document.createElement("IFRAME" ...

  3. golang 学习路径

    目录 一 了解 go 二 入门教程 三 安装运行环境 & IDE(goland) 四 gotour 五 简易源码解析 六 开始写代码 七 学习框架 八 惯用法 九 调优 一 了解 go 谷歌一 ...

  4. 【OJ】抓牛问题

    /* 农夫John的奶牛跑路了.将地图视作一条数轴,John的初始位置在s而奶牛的位置在t(0<=s,t<=100000).John可以花费一分钟的时间使自己作如下移动: 1 从点x移动到 ...

  5. 2019年春季学期第二周作业 基础作业 请在第一周作业的基础上,继续完成:找出给定的文件中数组的最大值及其对应的最小下标(下标从0开始)。并将最大值和对应的最小下标数值写入文件。 输入: 请建立以自己英文名字命名的txt文件,并输入数组元素数值,元素值之间用逗号分隔。 输出 在不删除原有文件内容的情况下,将最大值和对应的最小下标数值写入文件

    ~~~ include<stdio.h> include<stdlib.h> int main() { FILE*fp; int i=0,max=0,j=0,maxb=0; i ...

  6. docker制作tomcat镜像

    准备cestos镜像.tomcat.jdk cestos是docker pull下来的 在/opt/下创建docker:mkdir -p /opt/docker 把tomcat和jdk解压到docke ...

  7. file_get_contents("php://input")的用法

    $data = file_get_contents("php://input"); php://input 是个可以访问请求的原始数据的只读流. POST 请求的情况下,最好使用 ...

  8. 微信网页授权 通过code获取openid 报错40163 code been used

    使用好好的微信功能,突然安卓无法正常使用了,苹果的正常. 安卓报错内容: 40163,code been used. 题外话:微信的东西,为何报英文错误呢,装什么13. 实测结果:安卓获取用户信息时 ...

  9. SSMP一次请求数据处理过程分析

    控制器代码 @RequestMapping("/changeUserPwd") public TranMessage changeUserPwd(String oriPwd, St ...

  10. [Android] 对于com.google.gson.JsonElement的转义问题

    不多说了,com.google.gson.JsonElement使用的时候,toString()跟getAsString()这两个方法对于特殊字符的转义是不同的, 看这里的解释: https://st ...