<!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>
<!-- 需要注意这里的文件 -->
<script src="../js/vue.js"></script>
<script src="../js/vue-router.js"></script>
</head>
<style>
.active{
color: red;
}
</style>
<body>
<div id="app">
<ul>
<li>
<router-link active-class='active' to='/home'>home</router-link>
</li>
<li>
<router-link active-class='active' to='/second'>second</router-link>
</li>
<li>
<router-link active-class='active' to='/user/junkai'>junkai</router-link>
</li>
<li>
<router-link active-class='active' to='/user/yiming'>yiming</router-link>
</li>
<li>
<router-link active-class='active' to='/user/siyou'>siyou</router-link>
</li>
<li>
<router-link active-class='active' to='/goods/110/TCL'>电视商品</router-link>
</li>
</ul>
<router-view></router-view>
</div>
</body>
<script>
var HomeComponent = Vue.component('home-component', {
template: `
<div>
<div>我是Home</div>
</div>
`
});
var SecondComponent = Vue.component('second-component', {
template: `
<div>
<div>我是 SecondComponent</div>
</div>
`
});
var goodsComponent = Vue.component('second-component', {
template: `
<div>
<div>goodsComponent</div>
<div>{{$route.params.styleId}}</div>
<div>{{$route.params.brand}}</div>
<div></div>
</div>
`
});
var UserComponent = Vue.component('second-component', {
template: `
<div>
<div>我是 UserComponent</div>
<div>{{$route.params.username}}</div>
<div>{{userInfo}}</div>
</div>
`,
data: function () {
return {
userInfo: null
}
},
mounted: function () {
var username = this.$route.params.username;
var that = this;
console.log(username);
debugger
setTimeout(function () {
that.userInfo = {
username: 'siyou',
age: 18,
location: '长沙'
}
},1000);
}
});
var routes = [{
path: '/home',
component: HomeComponent
},{
path: '/second',
component: SecondComponent
},{
path: '/user/:username',
component: UserComponent
},{
path: '/goods/:styleId/:brand',
component: goodsComponent
},{
path: '*',
redirect: '/second'
}];
var router = new VueRouter({
'routes': routes
});
var app = new Vue({
el: '#app',
data: function () {
return {
name: 'hongyang'
}
},
'router': router
})
</script>
</html>