美文网首页
vuejs入门-子路由(三)

vuejs入门-子路由(三)

作者: 矢风 | 来源:发表于2017-07-16 08:11 被阅读0次

    上次说了整页导航,这次我们说页面局部导航
    局部导航白话就是:页面局部刷新或是视图局部刷新

    1.搭建项目:
    vue init webpack tj1
    2.按图创建文件:

    clipboard.png

    3.运行案例,然后看源码自己体会:
    index.html:
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>tj1</title>
    </head>
    <body>
    <div id="app">
    <router-view></router-view>
    </div>
    </body>
    </html>

    pages/First.vue:
    <template>
    <div>
    <span>第1个页面</span>
    <router-link to="/secod" >内容页</router-link>
    </div>
    </template>

    <script>
    export default {
    data () {
    return {
    }
    }
    }
    </script>

    <style scoped>
    span{
    font-weight: normal;
    color:red
    }
    </style>

    pages/Second.vue:
    <template>
    <div>
    <span>第2个页面</span>
    <router-link to="/" >首页</router-link>
    <router-view></router-view>
    <router-link to="/secod" >第1个子页面</router-link>
    <router-link to="/sec2" >第2个子页面</router-link>
    <router-link to="/sec3" >第3个子页面</router-link>
    </div>
    </template>

    <script>
    export default {
    data () {
    return {
    }
    }
    }
    </script>

    <style scoped>
    span{
    font-weight: normal;
    color:red
    }
    </style>

    pages/sec/Sec1.vue:
    <template>
    <span>第1个子页面</span>
    </template>

    <script>
    export default {
    data () {
    return {
    }
    }
    }
    </script>

    <style scoped>
    span{
    font-weight: normal;
    color:red
    }
    </style>

    pages/sec/Sec2.vue:
    <template>
    <span>第2个子页面</span>
    </template>

    <script>
    export default {
    data () {
    return {
    }
    }
    }
    </script>

    <style scoped>
    span{
    font-weight: normal;
    color:red
    }
    </style>

    pages/sec/Sec3.vue:
    <template>
    <span>第3个子页面</span>
    </template>

    <script>
    export default {
    data () {
    return {
    }
    }
    }
    </script>

    <style scoped>
    span{
    font-weight: normal;
    color:red
    }
    </style>

    main.js:
    import Vue from 'vue'
    import VueRouter from 'vue-router'
    import First from './pages/First'
    import Secod from './pages/Secod'

    import Sec1 from './pages/sec/Sec1'
    import Sec2 from './pages/sec/Sec2'
    import Sec3 from './pages/sec/Sec3'

    Vue.use(VueRouter)

    // 定义路由配置
    const routes = [
    {path: '/',component: First},
    {path: '/secod',
    component: Secod,
    children: [
    {path: '/',component: Sec1},
    {path: '/sec2',component: Sec2},
    {path: '/sec3',component: Sec3}
    ],
    }
    /*
    这么写就成了一级页面的跳转了,而不是二级局部跳转
    {path: '/Sec1',component: Sec1},
    {path: '/Sec2',component: Sec2},
    {path: '/Sec3',component: Sec3},
    */
    ]

    // 创建路由实例
    const router = new VueRouter({
    routes
    })

    // 创建 Vue 实例
    new Vue({
    el: '#app',/* 对应index.html的div的id="app" */
    data(){
    return {
    }
    },
    router
    })

    相关文章

      网友评论

          本文标题:vuejs入门-子路由(三)

          本文链接:https://www.haomeiwen.com/subject/afktkxtx.html