Vue Router 实现多种页面跳转

时间:2022-07-24
本文章向大家介绍Vue Router 实现多种页面跳转,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

这种跳转方式类似于传统的a 标签实现跳转

 <router-link to="./shop">Go Shop</router-link>

this.$router.push

用的较多的是使用方法进行跳转然后实现页面之间的传参

<div  @click="gohome">我要去home</div>
<script>
export default {
  data() {
    return {
      id:5
    };
  },
  methods: {
    gohome() {
      this.$router.push({
        path: "/home",
        query: {
          id: this.id
        }
      });
    }
  },
};
</script>

接收参数页面 在域名就会看到域名带?id=5

<script>
export default {
  data() {
    return {};
  },
  methods:{
  },
 created(){  //生命周期里接收参数
 	this.id = this.$route.query.id, 
    console.log(this.id)   
   }
};

this.$router.replace

用法跟this.$router.push一样,但是跳转有区别。 前者跳转之后会向history栈添加一个记录,点击后退会返回到上一个页面。 后者跳转不会向history里面添加新的记录,点击返回,会跳转到上上一个页面。上一个记录是不存在的。

this.$router.go()

类似于window.history.go(n)。

    // 在浏览器记录中前进一步,等同于 history.forward()
    this.$router.go(1)
    
    // 后退一步记录,等同于 history.back()
    this.$router.go(-1)