当前位置: 代码网 > it编程>编程语言>Javascript > Vue让router-view默认显示页面操作方法

Vue让router-view默认显示页面操作方法

2024年05月18日 Javascript 我要评论
vue让router-view默认显示页面操作方法场景:一个home页面,点击左边的菜单栏,右边显示页面,因此都知道在右边放一个router-view然后配置路由即可。然而问题出现在:重新打开的时候,

vue让router-view默认显示页面操作方法

场景:一个home页面,点击左边的菜单栏,右边显示页面,因此都知道在右边放一个router-view然后配置路由即可。

然而问题出现在:重新打开的时候,默认是白色空的。

那么解决办法是:

在router管理的index.js中
加上 redirect:'/xxxx'

{
    path: '/home',
    name: 'home',
    component: home,
    meta: {
      requireauth: true // 添加该字段,表示进入这个路由是需要登录的
    },
    redirect:'/wxuser',
    children: [{
      path: '/admin',
      name: '后台用户管理',
      component: admin,
      meta: {
        requireauth: true
      }
    },
      {
        path: '/wxuser',
        name: '微信用户管理',
        component: wxuser,
        meta: {
          requireauth: true
        }
      }]
  }

扩展:

vue-router默认的开始界面

①利用利用redirect单独写一个路由

  {
    path: "/",
    redirect: "/home",
  },
  {
    path: "/home",
    name: "home",
    component: () => import("../pages/home/index.vue"), //这里是路由懒加载,使访问更加高效
  },
  {
    path:"/login",
    name:"login",
    component: () => import("../pages/login/index.vue"),
  }

②router的children的初始页面,可以用redirect定义初始页面,也可以按①的方式

 {
    path: "/helloworld",
    name: "helloworld",
    component: helloworld,
    //进入时默认路由为"/1"
    redirect: "/1",
    children: [
      {
        path: "/1",
        name: "ahomepage",
        component: ahomepage,
      },
      {
        path: "/2",
        name: "one",
        component: one,
      },
      {
        path: "/3",
        name: "two",
        component: two,
      },
      {
        path: "/5",
        name: "nodeapi",
        component: nodeapi,
      },
      {
        path: "/4",
        name: "arcgis",
        component: arcgis,
      },
      {
        path: "/6",
        name: "try",
        component: try,
      },
      {
        path: "/7",
        name: "echarts",
        component: echarts,
      },
      {
        path: "/8",
        name: "geogis",
        component: geogis,
      },
      {
        path: "/9",
        name: "less",
        component: less,
      },
    ],
  },

到此这篇关于vue关于如何让router-view默认显示页面问题的文章就介绍到这了,更多相关vue router-view默认显示页面内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com