zoukankan      html  css  js  c++  java
  • vue-router路由的使用

    1、路由作用

    用vue.js + vue-router创建单页面应用。页面不需要刷新就可以页面跳转,提供用户更好体验。

    2、路由配置

    new Router({
        routes: [{
                path: '/', //匹配路径
                name: 'Hello', //路由的别名
                component: Hello //需要加载的组件名
            }, {
                //使用变量名的形式传递参数。例如:/detail/10086
                //在Detail组件中使用{{$router.params.id}}来接收
                path: '/detail/:id',
                name: 'Detail',
                component: Detail
            }
        ]
    })

     3、路由跳转

    <router-link :to="{name:'Detail',params:{id:10086}}">详情</router-link>

     4、实践:两组件之间跳转

    代码实例
    
    4.1、index.js
    import Vue from 'vue'
    import Router from 'vue-router'
    import Hello from '@/component/Hello'
    import Detail from '@/component/Detail'
    
    Vue.use(Router)
    
    export defaultnew Router({
        router: [{
                path: '/',
                name: 'Hello',
                component: Hello
            }, {
                path: '/detail/:id',
                name: 'Detail',
                component: Detail
            }]
    })
    
    4.2、Hello.vue:src->component->Hello.vue
    
     <template>
     <div class = "hello" >
         <h1>这是hello页面 </h1>
         < router - link: to = {name: 'Detail',params: {id: 10086}}>详情</router-link>
     </div>
     </template>
    
     <script>
    export default{
        name: 'hello',
        data() {
            return {
                msg: 'hello vue'
            }
        }
    }
     </script>
    
     < !--add "scoped" attribute limit css to this compent only-- >
     <style scoped>
        h1, h2 {
            font - weight: normal;
        }
    
        ul {
            list - style - type: none;
            padding: 0;
        }
    
        li {
            display: inline - block;
            margin: 0 10px;
        }
    
        a {
            color:  # 42b983
        }
        </style>
    
    4.3、Detail.vue:src->component->Detail.vue
    
     < template >
     < div >
     < h1 > 这是detail页面 </h1> 
     {{$route.params.id}}
     </div>
     </template>
    
     <script>
    export default{
        name: 'hello',
        data() {
            return {}
        }
    }
     </script>
    
     < !--add "scoped" attribute limit css to this compent only-- >
     < style scoped >
     </style>
    
    4.4、启动项目
    my_vue_app > npm run dev

     5、vue组件由三部分组成

    vue组件:template:html代码、 script:javascript代码、style:css代码

  • 相关阅读:
    今天,你ak了吗?①
    线段树模板
    DP(关于字符串,数字串的)
    Leedsdiscussion
    高数积分求旋转体体积
    tiny mission
    莫队+数组低级化的 优先队列
    LAB2
    Leedslecturepronouncation
    Eclipse Access Restriction
  • 原文地址:https://www.cnblogs.com/chenweichu/p/9302219.html
Copyright © 2011-2022 走看看