vue路由引數
阿新 • • 發佈:2018-12-26
<!DOCTYPE html>
<html lang="en">
<head>
<title></title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="./vue.js"></script>
<!-- 1. 先引入vue-router檔案 -->
< script src="./vue-router.js"></script>
<style>
ul {
display: flex;
}
li {
list-style-type: none;
padding: 0 20px;
}
</style>
</head>
<body>
<div id="app">
<ul>
<li><router-link to="/home">首頁</router-link></li>
<li><router-link to="/product/one">蔬菜</router-link></li>
<li><router-link to="/product/tow">水果</router-link></li>
<li><router-link to="/product/33">肉類</router-link></li>
</ ul>
<router-view></router-view>
</div>
<script>
let myhome = Vue.component('home', {
template: '<div>這是首頁</div>'
})
let myproduct = Vue.component('product', {
// 2. 頁面中獲取路由引數通過 $route.params.引數名 獲取
template: `
<div>
<p>商品分類,該分類的編號是{{$route.params.cid}}</p>
</div>
`,
mounted () {
// 3. js中通過this.$route.params.引數名來獲取路由引數
console.log(this.$route.params.cid);
}
})
let router = new VueRouter({
routes: [
{name: 'index', path: '/home', component: myhome},
// 1. 路由引數 :引數名 就可以定義了,⭐這個冒號的作用是將後面的變數變成引數
{name: 'productCate', path: '/product/:cid', component: myproduct}
]
})
var vm = new Vue({
el: '#app',
router,
data: {
}
})
</script>
</body>
</html>