vue 2.0 路由的基本使用:

2018-06-28  本文已影响26人  lvyweb

标签(空格分隔): vue


路由

1.  布局
    <router-link to="/home">主页</router-link>

    <router-view></router-view>
2. 路由具体写法
    //组件
    var Home={
        template:'<h3>我是主页</h3>'
    };
    var News={
        template:'<h3>我是新闻</h3>'
    };

    //配置路由
    const routes=[
        {path:'/home', component:Home},
        {path:'/news', component:News},
    ];

    //生成路由实例
    const router=new VueRouter({
        routes
    });

    //最后挂到vue上
    new Vue({
        router,
        el:'#box'
    });
3. 重定向
    之前  router.rediect  废弃了
    {path:'*', redirect:'/home'}

全部代码

    <body>
        <div id="box">
            <h1>Hello App!</h1>
            <p>
                <!-- 使用 router-link 组件来导航. -->
                <!-- 通过传入 `to` 属性指定链接. -->
                <!-- <router-link> 默认会被渲染成一个 `<a>` 标签 -->
                <router-link to="/home">主页</router-link>
                <router-link to="/news">新闻</router-link>
            </p>
            <!-- 路由出口 -->
            <!-- 路由匹配到的组件将渲染在这里 -->
            <router-view></router-view>
        </div>
        <script src="vue.js"></script>
        <script src="./dist/vue-router.js"></script>
        <script>
              //组件
            var Home={
                template:'<h3>我是主页</h3>'
            };
            var News={
                template:'<h3>我是新闻</h3>'
            };
            //配置路由
            const routes=[
                {path:'/home', component:Home},
                {path:'/news', component:News},
                {path:'*', redirect:'/home'} //重定向
            ];
            //生成路由实例
            const router=new VueRouter({
                routes
            });
            //最后挂到vue上
            new Vue({
                router,
                el:'#box'
            });
    </script>
</body>
</html>

这样运行就实现了一个简单的单页面应用

上一篇 下一篇

猜你喜欢

热点阅读