當在一個元件容器裡面進行路由跳轉(比如在個人賬號元件中跳轉登入元件和註冊元件)
- 使用children屬性,實現子路由
<!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="https://cdn.jsdelivr.net/npm/vue"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css"
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
</head>
<body>
<div id="app">
<router-link to="/main">main</router-link>
<router-view></router-view>
</div>
<template id="main">
<div>
<h1>main</h1>
<router-link to="/main/login">登入</router-link>
<router-link to="/main/register">註冊</router-link>
<!-- 元件裡再定義一個router-view -->
<router-view></router-view>
</div>
</template>
<script>
let login = { template: '<h1>login</h1>' }
let register = { template: '<h1>register</h1>' }
let main = { template: '#main' }
const router = new VueRouter({
routes: [
// { path: '/login', component: login },
// { path: '/register', component: register }
{
path: '/main',
component: main,
children: [
// 路徑不要加/
{ path: 'login', component: login },
{ path: 'register', component: register }
]
}
]
})
var vm = new Vue({
el: '#app',
data: {},
methods: {},
router
});
Vue.config.devtools = true
</script>
</body>
</html>
本作品採用《CC 協議》,轉載必須註明作者和本文連結