码迷,mamicode.com
首页 > 其他好文 > 详细

vue2.0路由-适合刚接触新手简单理解

时间:2018-05-29 13:16:19      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:redirect   meta   组件映射   lin   type   关于   ext   bar   ack   

vue路由:vue-router

vue-router是Vue.js官方的路由插件,它和vue.js是深度集成的,适合用于构建单页面应用。vue的单页面应用是基于路由和组件的,路由用于设定访问路径,并将路径和组件映射起来。传统的页面应用,是用一些超链接来实现页面切换和跳转的。在vue-router单页面应用中,则是路径之间的切换,也就是组件的切换。

下载方式:npm install vue-router

html:

 

技术分享图片
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>vue路由</title>
    <script src="vue.min.js"></script>
    <script src="vue-router.min.js"></script>
</head>
<body>
    <div id="box">
        <div>
            <router-link to="/home">主页</router-link>
            <router-link to="/news">新闻</router-link>
            <router-link to=‘/about‘>关于</router-link>
        </div>
        <div>
            <router-view></router-view>
        </div>
    </div>
</body>
</html>
技术分享图片

JavaScript:

技术分享图片
    <script>
        //组件
        const Home = {
            template:‘<h3>我是主页</h3>‘
        };
        const News = {
            template:‘<h3>我是新闻</h3>‘
        }
        const About = {
            template:‘<h3>我是关于</h3>‘
        }
        //配置路由
        const routes = [ 
            {path:‘/home‘, component :Home},
            {path:‘/news‘, component:News},
            {path:‘/about‘,component:About},
            //重定向
            {path:‘*‘,redirect:‘/home‘}
        ]
        //生成路由实例
        const router = new VueRouter({
            routes
        })
        //挂载到vue上
        new Vue({
            router,
            el:‘#box‘
        })
    </script>
技术分享图片

CSS:

 

技术分享图片
<style>
    .router-link-active{
        background: #ccc;
        padding: 5px;
        text-decoration: none;
    }
</style>
技术分享图片

总体:

技术分享图片
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>vue路由</title>
    <script src="vue.min.js"></script>
    <script src="vue-router.min.js"></script>
    <style>
        .router-link-active{
            background: #ccc;
            padding: 5px;
            text-decoration: none;
        }
    </style>
</head>
<body>
    <div id="box">
        <div>
            <router-link to="/home">主页</router-link>
            <router-link to="/news">新闻</router-link>
            <router-link to=‘/about‘>关于</router-link>
        </div>
        <div>
            <router-view></router-view>
        </div>
    </div>

    <script>
        //组件
        const Home = {
            template:‘<h3>我是主页</h3>‘
        };
        const News = {
            template:‘<h3>我是新闻</h3>‘
        }
        const About = {
            template:‘<h3>我是关于</h3>‘
        }
        //配置路由
        const routes = [ 
            {path:‘/home‘, component :Home},
            {path:‘/news‘, component:News},
            {path:‘/about‘,component:About},
            //重定向
            {path:‘*‘,redirect:‘/home‘}
        ]
        //生成路由实例
        const router = new VueRouter({
            routes
        })
        //挂载到vue上
        new Vue({
            router,
            el:‘#box‘
        })
    </script>
</body>
</html>

vue2.0路由-适合刚接触新手简单理解

标签:redirect   meta   组件映射   lin   type   关于   ext   bar   ack   

原文地址:https://www.cnblogs.com/aacoutlook/p/9104482.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!