<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
</head>
<body>
<div id="app">
<h1>{{ msg }}</h1>
<hr />
<router-link to="/product/1/haha">商品1</router-link>
<router-link to="/product/2">商品2</router-link>
<router-link to="/product/3">商品3</router-link>
<router-link to="/product/4">商品4</router-link>
<hr />
<router-view></router-view>
</div>
<script src="vue.js"></script>
<script src="vue-router.js"></script>
<script>
const Product = {
template: `
<div>这是商品详情页的组件 -----{{$route.params.id}}</div>
`,
created() {
console.log(this.$route)
}
}
// 动态路由的目的:让多个路由规则匹配同一个组件
// 1. 定义规则 { path: '/product/:id' , component: Product}
// 2. 获取动态路由的参数: this.$route.params.id
const router = new VueRouter({
// 动态路由匹配
// /product/:id 匹配到所有 /product/10086
routes: [{ path: '/product/:id/:aa', component: Product }]
})
const vm = new Vue({
el: '#app',
data: {
msg: 'hello vue'
},
router
})
</script>
</body>
</html>
$route详解
最新推荐文章于 2024-08-12 17:17:04 发布