-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvue_router_04.html
77 lines (73 loc) · 1.83 KB
/
vue_router_04.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<style rel="stylesheet">
.router-link-active {
color: #FF0000;
}
</style>
</head>
<body>
<div id="container">
<h3>Hello Vue-Router---编程导航</h3>
<div>
<router-link to="/person/3">person---3</router-link>
<br>
<button id="btn1">person---4---add</button>
<br>
<button id="btn2">person---5---update---6</button>
</div>
<router-view></router-view>
</div>
<script src="https://unpkg.com/vue"></script>
<script src="https://unpkg.com/vue-router"></script>
<script>
var personComponent = {
template: "<div class='person'><span>person {{$route.params.id}}</span><br><router-view></router-view></div>"
};
var defaultComponent = {
template: "<div class='home'>default content</div>"
}
var addComponent = {
template: "<div class='add'>add content</div>"
};
var updateComponent = {
template: "<div class='update'>update content {{$route.params.name}}</div>"
}
const current_routes = [
{
path: '/person/:id',
component: personComponent,
children: [
{
path: "",
component: defaultComponent
}, {
path: "add",
component: addComponent
}, {
path: "update/:name",
component: updateComponent
}
]
}
]
const current_router = new VueRouter({
routes: current_routes
})
var app = new Vue({
router: current_router
}).$mount('#container');
/* 响应事件必须写在Vue实例初始化之后 */
var btn1 = document.querySelector("#btn1"), btn2 = document.querySelector("#btn2");
btn1.onclick = function () {
current_router.push({path: "/person/4/add"});
};
btn2.onclick = function () {
current_router.push("/person/5/update/6");
};
</script>
</body>
</html>