vue-入坑--嵌套路由 导致 页面跳转 为空问题(子页面内容不显示)

vue-入坑–嵌套路由导致页面跳转为空问题(页面不显示内容)

遇到的问题

vue 空白页面
使用嵌套子路由切换页面,地址栏有变化,但是子页面内容没有显示(一片空白)。

上代码~~

嵌套子路由代码 src->router->index.js
//...
import Detail2 from '@/components/Detail2'
import Details from '@/components/Details'
//...
export default new Router({
  //  去掉 http://localhost:8080/#的#
  mode: 'history',
  routes: [
    {
      path: '/',
      name: 'myHello',
      component: MyHello
    },
    {
      path: '/Detail2',
      name: 'Detail2',
      component: Detail2,
      children: [
        {
          path: '/Details/:idvv',
          name: 'Details',
          component: Details
        }
      ]
    } 
  ]
})
Detail2.vue (父页面)
<template>
  <div class="hello">
    <h1>这是一个父页面</h1>
    <!-- 动态路由的to里不能写path,不然就不管用了 -->
    <ul  v-for="item in question" >
    <li>
       <router-link :to="{name: 'Details', params: {idvv: item.id}}">{{item.info}}</router-link>
    </li>
    </ul>  
  </div>
</template>
<script>

    export default {
        data (){
          return {
            question: [
               {
                id: 1,
                info: 'About question1'
                },
              {
                id:2,
                info: 'About question2'
              },
              {
                id:3,
                info: 'About question3'
              }
            ]
          }
        }
    }


</script>
Details.vue (子页面)
<template>
    <div class="hello">
       <h1>这是一个子页面</h1>
        <h4>问题{{$route.params.idvv}}</h4>
        <div>{{questionInfo}}</div>
        <button @click="handleClick" v-show="flag">下一个问题</button>
    </div>
</template>
<script>
export default {
    name: 'details隨便命名',
    beforeRouteUpdate (to, from, next){
        this.getData(to.params.idvv)
        next();
    },
    mounted () {
        this.getData(this.$route.params.idvv)
    },
    data () {
        return {
            questionInfo: '',
            curId: '',
            flag: true,
            question: [
              {
                id: 1,
                title: '今天吃什么?'
              },
                            {
                id: 2,
                title: '好吃吗?'
              },
                            {
                id: 3,
                title: '待会打打麻将?'
              }
            ]
        }
    },
    methods: {
        handleClick() {
            this.$router.push({
                name: 'Details',
                params: {
                    idvv: this.curId + 1
                }
            })
        },
        getData (id){
            const index = this.question.findIndex(item => item.id === id);
            console.log(index);
            if(index == -1){
                this.flag = false;
            }else{
                this.questionInfo = this.question[index].title;
                this.curId = id;
                this.flag = true;
            }
        }
    }
}
</script>

解决的方法

方法1:父页面添加子页面挂载的位置点 <router-view/>

Detail2.vue (父页面)
<template>
  <div class="hello">
    <h1>这是一个父页面</h1>
    <!-- 动态路由的to里不能写path,不然就不管用了 -->
    <ul  v-for="item in question" >
    <li>
       <router-link :to="{name: 'Details', params: {idvv: item.id}}">{{item.info}}</router-link>
    </li>
    </ul>
   <br/>  
   <router-view/>
  </div>
</template>
<script>

    export default {
        data (){
          return {
            question: [
               {
                id: 1,
                info: 'About question1'
                },
              {
                id:2,
                info: 'About question2'
              },
              {
                id:3,
                info: 'About question3'
              }
            ]
          }
        }
    }


</script>

知识点:<router-view> 组件是一个 functional 组件,渲染路径匹配到的视图组件。<router-view> 渲染的组件还可以内嵌自己的 <router-view>,根据嵌套路径,渲染嵌套组件。

方法2:

改动动态路由位置,不用嵌套路由,让子页面和父页面同级显示

src->router->index.js
export default new Router({
  //  去掉 http://localhost:8080/#的#
  mode: 'history',
  routes: [
    {
      path: '/',
      name: 'myHello',
      component: MyHello
    },
    {
      path: '/Detail2',
      name: 'Detail2',
      component: Detail2
    },
    {
      path: '/Details/:idvv',
      name: 'Details',
      component: Details
    } 
  ]
})

总结:路由是按定义的顺序执行的,谁先定义就先用谁的规则

1.上面如果父级路由和嵌套子级路由都配置了,那么会先执行父级路由,因为它放在了上面位置;
1.同理类推:如果它放在了下面位置,那么就先执行嵌套子路由规则。

刚开始学,是最容易遇见问题和解决问题的时候,越是最容易纠结和释怀的时候,希望本文对初入学者有一点点的帮助!【语来了】

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Vue Router 是 Vue.js 官方的路由管理器。它和 Vue.js 的核心深度集成,可以非常方便的实现单页面应用程序的路由功能。 Vue Router 提供了以下两种方式实现页面路由跳转: 1. 声明式导航:通过 `<router-link>` 组件实现页面跳转,该组件会被渲染成一个 `<a>` 标签,点击该标签后会触发路由跳转。 2. 编程式导航:通过 `$router` 对象提供的方法实现页面跳转,例如 `$router.push()`、`$router.replace()`、`$router.go()` 等方法。 下面我们来看一个简单的示例,在 Vue Router 中如何实现页面路由跳转: ```html <!-- 在模板中使用 router-link 组件 --> <router-link to="/home">Home</router-link> <!-- 在组件中使用 $router 对象实现编程式导航 --> <button @click="$router.push('/about')">About</button> ``` 在上面的示例中,我们分别演示了如何使用 `<router-link>` 组件和 `$router` 对象实现页面路由跳转,其中 `to` 属性表示跳转的目标路由地址。 此外,Vue Router 还支持路由嵌套的功能,可以通过嵌套路由的方式实现页面嵌套展示。下面我们来看一个嵌套路由的示例: ```javascript const router = new VueRouter({ routes: [ { path: '/home', component: Home, children: [ { path: 'news', component: News }, { path: 'videos', component: Videos } ] } ] }) ``` 在上面的示例中,我们定义了一个名为 `Home` 的路由组件,并在该组件中嵌套了两个路由 `News` 和 `Videos`,这样就可以实现在 `Home` 组件中展示新闻和视频内容的功能。在实际开发中,我们可以根据实际需求来定义不同的路由组件和路由,实现灵活的页面嵌套效果。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值