vue中的路由深入了解和路由传参

目录

什么是路由

路由跳转的用法第一种方法(声明式)

路由跳转的用法第二种方法(编程式)

路由的传参方式第一种方法

路由的传参方式第二种方法


什么是路由

vue路由是根据url分配对应的处理程序;点击链接,修改url地址,路由监听到url地址改变后,进行路由规则匹配,匹配到后通过router-view来显示对应组件。


路由跳转的用法第一种方法(声明式)

1.在页面中定义:

<template>
  <div>
    <!-- 使用router-link 路由组件跳转(相当于链接) -->
    <!-- 通过to 可以跳转到指定的页面 -->

    <!-- 常见的方式-->
    <router-link to="/List">跳转到List页面</router-link>
    <router-link to="/About">跳转到About页面</router-link>
    
    <!-- 通过对象的方式跳转 -->
    <router-link :to="{ path: '/List' }">跳转到List页面</router-link>
    <router-link :to="{ path: '/About' }">跳转到About页面</router-link>

    <!-- 通过路由名字的方式跳转 -->
    <router-link :to="{ name: 'List' }">跳转到List页面</router-link>
    <router-link :to="{ name: 'About' }">跳转到About页面</router-link>

    <!-- router-view 路由出口 将路由匹配到的组件显示到里面 -->
    <router-view></router-view>
  </div>
</template>

 2.在router.js中定义路由

import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect: '/About',    //重定向
  },
  {
    path: "/About",
    name: "About",
    component: () => import("../views/About/index.vue"),
  },
  {
    path: "/List",
    name: "List",
    component: () => import("../views/List/index.vue"),
  },
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
  linkActiveClass: 'active'
});

export default router;

路由跳转的用法第二种方法(编程式)

1.在页面中定义:

<template>
  <div>
    <button @click="goAbout">跳转到About页面</button>
    <button @click="goList">跳转到List页面</button>
  </div>
</template>

<script>
export default {
  data() {
    return {};
  },

  methods: {
    goAbout() {
      this.$router.push("/About");
    },
    goList() {
      this.$router.push("/List");
    },
  },
};
</script>

  2.在router.js中定义路由

import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect: '/About',    //重定向
  },
  {
    path: "/About",
    name: "About",
    component: () => import("../views/About/index.vue"),
  },
  {
    path: "/List",
    name: "List",
    component: () => import("../views/List/index.vue"),
  },
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
  linkActiveClass: 'active'
});

export default router;

路由的传参方式第一种方法

1.在要跳转的页面中定义:

<template>
  <div>
    
    <!-- 第一种:直接传参 http://localhost:8080/My/100 (this.$route.params 获取方法)-->
    <router-link :to="'/My/' + 100">跳转到My页面</router-link>

    <!-- 第二种:query传参 http://localhost:8080/About?id=10&name=xq -->
    <router-link :to="{ path: '/About', query: { id: 10, name: 'xq' } }">跳转到About页面</router-link>
    <router-link :to="{ name: 'About', query: { id: 10, name: 'xq' } }">跳转到About页面</router-link>

    <!-- 第三种:params传参 http://localhost:8080/List/10/xq (params传参对地址路由path不生效,只对命名路由name生效)-->
    <router-link :to="{ path: '/List', params: { id: 10, name: 'xq' } }">跳转到List页面</router-link>
    <router-link :to="{ name: 'List', params: { id: 10, name: 'xq' } }">跳转到List页面</router-link>

  </div>
</template>

 2.在跳转到的页面中获取所带的参数:

<template>
  <div>
    <h1>{{$route.params.myId}}</h1>
    <h1>{{$route.query.id}}--{{$route.query.name}}</h1>
    <h1>{{$route.params.id}}--{{$route.query.name}}</h1>
  </div>
</template>

<script>
export default {
  data() {
    return {};
  },
  mounted() {
    console.log(this.$route.params.myId);
    console.log(this.$route.query.id, this.$route.query.name);
    console.log(this.$route.params.id,this.$route.params.name)
  },
};
</script>

3.在router.js中定义路由

import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect: '/About',    //重定向
  },
  {
    path: "/About",
    name: "About",
    component: () => import("../views/About/index.vue"),
  },
  {
    path: "/List/:id/:name",  //params需要  path不用
    name: "List",
    component: () => import("../views/List/index.vue"),
  },
  {
    path: "/My/:myId",
    name: "My",
    component: () => import("../views/My/index.vue"),
  },
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
  linkActiveClass: 'active'
});

export default router;

路由的传参方式第二种方法

1.在要跳转的页面中定义:

<template>
  <div>
    <button @click="goAbout">跳转到About页面</button>
    <button @click="goList">跳转到List页面</button>
  </div>
</template>

<script>
export default {
  data() {
    return {};
  },

  methods: {
    goAbout() {
      this.$router.push({
        path: "/About",
        query: {
          id: 1,
          name: "xq",
        },
      });
    },
    goList() {
      this.$router.push({
        name: "List",
        query: {
          id: 1,
          name: "XQ",
        },
      });
    },
  },
};
</script>

 2.在跳转到的页面中获取所带的参数:

<template>
  <div>
    <h1>{{$route.query.id}}--{{$route.query.name}}</h1>
    <h1>{{$route.params.id}}--{{$route.query.name}}</h1>
  </div>
</template>

<script>
export default {
  data() {
    return {};
  },
  mounted() {
    console.log(this.$route.query.id, this.$route.query.name);
    console.log(this.$route.params.id,this.$route.params.name)
  },
};
</script>

 3.在router.js中定义路由

import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect: '/About',    //重定向
  },
  {
    path: "/About",
    name: "About",
    component: () => import("../views/About/index.vue"),
  },
  {
    path: "/List",
    name: "List",
    component: () => import("../views/List/index.vue"),
  },
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
  linkActiveClass: 'active'
});

export default router;

后续 (防止导航出现问题)

解决办法 在router.js中

import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

//保存下来push方法 将来我们还会用到原push方法进行路由跳转
let originPush = Router.prototype.push;
//接下来我们重写push|replace方法
//第一个参数location参数像原push方法声明跳转的地方 resolve和reject传递成功与失败

Router.prototype.push = function(location, resolve, reject) {
  if (resolve && reject) {
    //如果成功 调用原来的push方法
    originPush.call(this, location, resolve, reject);
  } else {
    originPush.call(this, location, () => {}, () => {});
  }
}

路由简单拦截

import Vue from 'vue'
import VueRouter from 'vue-router'
import { Message } from 'element-ui';

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: () => import('../login/index.vue'),
  },
  {
    path: '/layout',
    name: 'layout',
    redirect: '/home',
    meta: { title: '首页' },
    component: () => import("../layout/index.vue"),
    children: [
      {
        path: "/home",
        name: "home",
        component: () => import("../views/home/index.vue"),
      },
      {
        path: "/tableList",
        name: "tableList",
        meta: { title: 'vue入门实例' },
        redirect: '/crud',
        component: () => import("../views/tableList/index.vue"),
        children: [
          {
            path: "/crud",
            name: "crud",
            meta: { title: '增删改查' },
            component: () => import("../views/tableList/crud.vue"),
          },
          {
            path: "/shopCar",
            name: "shopCar",
            meta: { title: '购物车' },
            component: () => import("../views/tableList/shopCar.vue"),
          },
        ]
      },
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

//路由拦截
router.beforeEach((to, from, next) => {
  if (to.path == '/login') {
    next()
  } else if (sessionStorage.getItem('username')) {
    next()
  } else {
    Message({
      showClose: true,
      message: '请先登录',
      type: 'warning'
    });
    next('/login')
  }
})
export default router
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值