vue2.0學習筆記之路由(二)路由嵌套


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>vue2.0學習筆記之路由(二)路由嵌套</title>
</head>
<body>
    <div id="app">
        <div>
            <router-link to="/home">主頁</router-link>
            <router-link to="/user">用戶中心</router-link>
        </div>
        <div>
            <router-view></router-view>
        </div>
    </div>
</body>
</html>
<script src="vue.js"></script>
<script src="vue-router.js"></script>
<script>
    // 定義組件
    var Home = {
        template:"<h3>主頁內容</h3>"
    }
    var User = {
        template:`
            <div>
                <h3>用戶中心</h3>
                <ul>
                    <li><router-link to="user/userinfo">查看個人信息</router-link></li>
                </ul>
                <div>
                    <router-view></router-view>
                </div>
            </div>

        `
    }
    var UserDetail = {
        template:"<h4>個人信息</h4>"
    }
    // 配置路由
    const routes = [
        { path:"/home", component:Home },
        {
            path:"/user",
            component:User,
            children:[  // 配置子路由
                { path:"userinfo",component:UserDetail }
            ]
        },
        { path:"*", redirect:"/home" } // 重定向讓頁面加載即顯示Home
    ]
    // 生成路由實例
    const router = new VueRouter({
        routes
    })
    // 掛載到vue實例上
    new Vue({
        el:"#app",
        router
    })
</script>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM