详解详解vue嵌套路由嵌套路由-params传递参数传递参数
本篇文章主要介绍了详解vue嵌套路由-params传递参数,小编觉得挺不错的,现在分享给大家,也给大家做个
参考。一起跟随小编过来看看吧
在嵌套路由中,父路由向子路由传值除了query外,还有params,params传值有两种情况,一种是值在url中显示,另外一种
是值不显示在url中。
1、显示在url中
index.html
<div id="app">
<!-- router-view 路由出口, 路由匹配到的组件将渲染在这里 -->
<router-view></router-view>
</div>
main.js params传值是通过 :[参数值] 如path: "/home/game/:num"
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
//引入两个组件
import home from "./home.vue"
import game from "./game.vue"
//定义路由
const routes = [
{ path: "/", redirect: "/home" },//重定向
{
path: "/home", component: home,
children: [
{ path: "/home/game/:num", component: game }
]
}
]
//创建路由实例
const router = new VueRouter({routes})
new Vue({
el: '#app',
data: {
id:123,
},
methods: {
},
router
})
home.vue 在home中具体的值就跟在路径后面,如 “/home/game/123”,也就是说传递给子路由的值就是 123
<template>
<div>
<h3>首页</h3>
<router-link to="/home/game/123">
<button>显示</button>
</router-link>
<router-view></router-view>
</div>
</template>
game.vue 在子路由中,通过 this.$route.params.参数名来接受传递过来的值
<template>
<h3>王者荣耀{{ this.$route.params.num }}</h3>
</template>
评论10