Vue-router浏览器历史记录的模式和编程式导航

浏览器历史记录

  1. 作用:控制路由跳转时操作浏览器历史记录的模式
  2. 浏览器的历史记录有两种写入方式:分别为pushreplacepush是追加历史记录,replace是替换当前记录。路由跳转时候默认为push
  3. 如何开启replace模式:
    <router-link replace .......>News</router-link>

声明式导航

        在浏览器中,点击链接实现导航的方式,叫做声明式导航。

        例如:普通网页中点击a链接,vue项目中点击<router-link>都属于声明式导航

<!-- 通过标签进行跳转 -->
<router-link to="/path">tab2</router-link>

编程式导航

      在浏览器中,调用API方法实现导航的方式,叫做编程式导航。

      例如:普通网页中调用location.href 跳转到新页面的方式,都属于编程式导航

  1. 作用:不借助<router-link>实现路由跳转,让路由跳转更加灵活
    <template>
      <div>
        <ul>
          <li v-for="item in list" :key="item.id">
            <router-link 
              replace 
              :to="{
              name: 'xiangqing',
              params: {
                id: item.id,
                title: item.title
              }
            }">{{ item.title }}</router-link>
            <button @click="examinePush(item)">push查看</button>
            <button @click="examineReplace(item)">replace查看</button>
          </li>
        </ul>
        <router-view></router-view>
      </div>
    </template>
    
    <script>
    export default {
      name: "Message",
      data() {
        return {
          list: [
            {
              id: "001",
              title: "消息001",
            },
            {
              id: "002",
              title: "消息002",
            },
            {
              id: "003",
              title: "消息003",
            },
          ]
        };
      },
      methods: {
        examinePush(item) {
          this.$router.push({
            name: 'xiangqing',
            params: {
              id: item.id,
              title: item.title
            }
          })
        },
        examineReplace(item) {
          this.$router.replace({
            name: 'xiangqing',
            params: {
              id: item.id,
              title: item.title
            }
          })
        }
      },
    };
    </script>
    

  2. 利用$router下面的方法可以实现路由的前进和后退

    this.$router.forward() //前进
    this.$router.back() //后退
    this.$router.go() //可前进也可后退

    代码演示

    <template>
    	<div class="col-xs-offset-2 col-xs-8">
    		<div class="page-header">
    			<h2>Vue Router Demo</h2>
    			<button @click="back">后退</button>
    			<button @click="forward">前进</button>
    			<button @click="test">测试一下go</button>
    		</div>
    	</div>
    </template>
    
    <script>
    	export default {
    		name:'Banner',
    		methods: {
    			back(){
    				this.$router.back()
    				// console.log(this.$router)
    			},
    			forward(){
    				this.$router.forward()
    			},
    			test(){
    				this.$router.go(3)  //可前进也可后退  正数前进 负数后退
    			}
    		},
    	}
    </script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李公子丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值