欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue如何通過點擊事件實現(xiàn)頁面跳轉(zhuǎn)詳解

 更新時間:2022年07月10日 10:05:47   作者:瘋狂的小強呀  
頁面跳轉(zhuǎn),我們一般都通過路由跳轉(zhuǎn)實現(xiàn),通常情況下可直接使用router-link標簽實現(xiàn)頁面跳轉(zhuǎn),下面這篇文章主要給大家介紹了關(guān)于vue如何通過點擊事件實現(xiàn)頁面跳轉(zhuǎn)的相關(guān)資料,需要的朋友可以參考下

前言

頁面跳轉(zhuǎn),我們一般都通過路由跳轉(zhuǎn)實現(xiàn),通常情況下可直接使用router-link標簽實現(xiàn)頁面跳轉(zhuǎn),但是如果我們想通過點擊別的標簽實現(xiàn)頁面跳轉(zhuǎn),怎么辦呢?這個時候我們就要用到this.$router.push()方法,下面來給大家簡單介紹一下使用!

this.$router.push()

1.首先我們要定義一個點擊事件

2.在定義事件中調(diào)用this.$router.push()方法

<template>
    <button @click = "handle">點擊跳轉(zhuǎn)</button>
</template>
<script>
    export default{
        methods:{
            handle (){
            //  路徑/home對應(yīng)我在router目錄下index.js中定義的path屬性值
                this.$router.push('/home');
            }
        }
    }
</script>

目標跳轉(zhuǎn)頁面路由在router目錄下index.js定義如下:

export default new Router({
  routes: [
    {
      path: '/home',
      name:'Home',
      component: Home,
    },
 ]
})

this.$router.push()中的參數(shù)規(guī)則

參數(shù)為字符串,即路徑名稱

//  路徑/home對應(yīng)router目錄下index.js中定義的path屬性值
this.$router.push('/home');

參數(shù)為對象

//  對應(yīng)router目錄下index.js中定義的path
this.$router.push({path:'/home'});

參數(shù)為路由命名

//  對應(yīng)router目錄下index.js中定義的name
this.$router.push({name:'Home'});

帶傳遞參數(shù)

// params里面放置的是我們要傳遞過去的參數(shù)
this.$router.push({name:'Home',params:{user:'david'}});

帶查詢參數(shù)

// 帶查詢參數(shù),傳遞過去的內(nèi)容會自動拼接變成/home?user=david
this.$router.push({path:'/home',query:{user:'david'}});

參數(shù)的接收

當我們使用params進行傳參時,只需在接收參數(shù)的地方使用this.$route.params進行接收即可

eg:

//傳參
this.$router.push({name:'Home',params:{user:'david'}});

// 在name為Home的組件中接收參數(shù)
const id=this.$route.params.id;
console.log(this.$route.params);//打印結(jié)果為{user:'david'}

當我們使用query傳參時,只需在接收參數(shù)的地方使用this.$route.query進行接收即可,用法同上

?。?!這里有一個小細節(jié):$符號后面跟的是route不是router,跳轉(zhuǎn)的時候 $后面跟的是router?。?!

注意

  • query傳參的參數(shù)會帶在url后邊展示在地址欄(/home?user=david),params傳參的參數(shù)不會展示到地址欄
  • 由于動態(tài)路由也是傳遞params的,所以在 this.$router.push() 方法中path不能和params一起使用,否則params將無效,需要用name來指定頁面
  • 我們也可以用this.$router.replace()來實現(xiàn)頁面跳轉(zhuǎn),二者的區(qū)別是push跳轉(zhuǎn)之后可以通過瀏覽器的回退鍵回到原來的頁面,而一旦使用replace跳轉(zhuǎn)之后,無法回到原來頁面

補充:VUE實現(xiàn)從一個頁面跳轉(zhuǎn)到另一個頁面的指定位置

例如,從網(wǎng)站的首頁點擊跳轉(zhuǎn)到指定頁面的底部。

首頁 home

<div @click="toPath('/targetPage#target')">
    <p>點擊跳轉(zhuǎn)</p>
</div>
methods:{
    //點擊跳轉(zhuǎn)方法
    toPath(path) {
        this.$router.push({path: path})
    }
}

跳轉(zhuǎn)到的頁面 targetPage

<div class="location" id="target">
    <p>指定位置</p>
</div>
//在mounted里
mounted() {
    var hash = window.location.hash;
    var index = hash.lastIndexOf("#");
    if (index != -1) {
       var id = hash.substring(index + 1, hash.length + 1);
       var div = document.getElementById(id);
       if (div) {
         setTimeout(function () {
           console.log($(div).offset().top);
           $('html, body').animate({scrollTop: $(div).offset().top - 43}, 500)
         }, 500);
       }
    }
}

親測有效 :D

總結(jié)

到此這篇關(guān)于vue如何通過點擊事件實現(xiàn)頁面跳轉(zhuǎn)的文章就介紹到這了,更多相關(guān)vue點擊事件實現(xiàn)頁面跳轉(zhuǎn)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論