本帖最后由 sukiwhip 于 2020-2-14 21:31 编辑 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://cn.vuejs.org/js/vue.min.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
<title>test</title>
</head>
<body>
<div id="root">
<router-view></router-view>
<div id="music">
<span>模拟歌曲播放计时:</span>
<span v-text="musicTime"></span>
</div>
</div>
</body>
<template id="home">
<div>
<router-link to="/home/1">跳到页面1</router-link>
<br><br>
<router-link to="/home/2">跳到页面2</router-link>
<br><br>
<div>注意,点击后 url 很明显是有变化,路由跳转了,把这条线上面的内容自由选择咔嚓掉,就是两个单独的页面,里面路由再怎么跑自己喜欢就好了,下面的音乐计时在全局上,一点都不影响</div>
<hr>
<router-view></router-view>
</div>
</template>
<template id="one">
<div>
我是页面1
</div>
</template>
<template id="two">
<div>
我是页面2
</div>
</template>
<script>
const router = new VueRouter({
routes: [{
path: "/home",
component: {
template: "#home"
},
children: [{
path: "1",
component: {
template: "#one"
}
}, {
path: "2",
component: {
template: "#two"
}
}]
}, {
path: "*",
redirect: "/home/1"
}]
})
new Vue({
el: "#root",
router,
data() {
return {
musicTime: 0
}
},
methods: {
musicGo() {
let that = this;
setInterval(function () {
that.musicTime++
}, 1000)
}
},
mounted() {
this.musicGo()
}
})
</script>
<style>
#music {
position: fixed;
bottom: 100px;
color: #f00;
}
</style>
</html>
直接给你个 demo,把最佳答案抱走 |