浅谈vue-router 路由传参的方法

2020-06-16 06:13:10易采站长站整理

<template id="reg">
<div>注册界面</div>
</template>

<template id="NewsDetail">
<div>
新闻详细页面
<span>{{$route.params.id}}</span>
</div>
</template>

<script type="text/javascript">
// 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>
</body>
</html>