vue Router路由配置步骤

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>
</head>

<body>
    <div id="app">
        <div class="head">这是头部</div>
        <div class="left">
            <ul>
                <li><a href="#/foo">Foo</a></li>
                <li><a href="#/bar">Bar</a></li>

                <!-- 使用 router-link 组件来导航. -->
                <!-- 通过传入 `to` 属性指定链接. -->
                <!-- <router-link> 默认会被渲染成一个 `<a>` 标签 -->
                <li>
                    <router-link to="/foo">Go to Foo</router-link>

                </li>
                <li>
                    <router-link to="/bar">Go to Bar</router-link>
                </li>
            </ul>
        </div>
        <div class="main">
            <!-- 4.定义路由出口 -->
            <!-- 路由匹配到的组件将渲染在这里 -->
            <router-view></router-view>

            333
        </div>
    </div>
    <script src="./node_modules/vue/dist/vue.js"></script>
    <script src="./node_modules/vue-router/dist/vue-router.js"></script>
    <script>
        //1.定义组件
        const Foo = {
            template: '<div>我是Foo组件</div>'
        }
        const Bar = {
            template: '<div>我是Bar组件</div>'
        }
        //2.定义路由并创建VueRouter实例
        const router = new VueRouter({
            routes: //注意单词是routes  而不是 router
            [ 
                { path: '/foo', component: Foo },
                { path: '/bar', component: Bar }
            ]
        })
        //3.挂载路由实例
        new Vue({
            el: '#app',
            router
        })
    </script>
</body>

</html>

  

posted on 2021-06-06 08:50  水行者  阅读(365)  评论(0编辑  收藏  举报

导航