编程式路由导航

<template>
  <div>
    <ul>
      <li v-for="item in messages" :key="item.id">
        <!--向message详情组件传参,使用query参数-->
        <router-link :to="`/home/message/detail/?id=${item.id}`">{{item.title}}</router-link>
        <!--编程式路由导航-->
        <button @click="pushShow(item.id)">push查看</button>
        <button @click="replaceShow(item.id)">replace查看</button>
      </li>
    </ul>
    <!--返回-->
    <button @click="$router.back()">返回</button>
    <hr>
    <router-view msg="aaa"></router-view>
  </div>
</template>

<script>
export default {
  data () {
    return {
      messages: []
    }
  },
  methods: {
    pushShow (id) {
      // 先进后出
      this.$router.push(`/home/message/detail?id=${id}`)
    },
    replaceShow (id) {
      // 先进先出
      this.$router.replace(`/home/message/detail?id=${id}`)
    }
  },
  mounted () {
    setTimeout(() => {
      this.messages = [
        {
          id: 1,
          title: 'message01'
        },
        {
          id: 2,
          title: 'message02'
        },
        {
          id: 3,
          title: 'message03'
        }
      ]
    }, 1000)
  }
}
</script>

<style scoped>

</style>

 

posted @ 2020-04-11 19:34  牛牛的自留地  阅读(154)  评论(0编辑  收藏  举报