vue2.0 點擊跳轉傳參--vue路由跳轉傳參數

來源:互聯網
上載者:User

vue中路由跳轉傳參數有多種,自己常用的是下面的幾種 通過router-link進行跳轉 通過編程導航進行路由跳轉

1. router-link

<router-link     :to="{        path: 'yourPath',         params: {             name: 'name',             dataObj: data        },        query: {            name: 'name',             dataObj: data        }    }"></router-link> 1. path -> 是要跳轉的路由路徑,也可以是路由檔案裡面配置的 name 值,兩者都可以進行路由導航 2. params -> 是要傳送的參數,參數可以直接key:value形式傳遞 3. query -> 是通過 url 來傳遞參數的同樣是key:value形式傳遞 // 2,3兩點皆可傳遞
2. $router方式跳轉

// 組件 a<template>    <button @click="sendParams">傳遞</button></template><script>  export default {    name: '',    data () {      return {        msg: 'test message'      }    },    methods: {      sendParams () {        this.$router.push({            path: 'yourPath',             name: '要跳轉的路徑的 name,在 router 檔案夾下的 index.js 檔案內找',            params: {                 name: 'name',                 dataObj: this.msg            }            /*query: {                name: 'name',                 dataObj: this.msg            }*/        })      }    },    computed: {    },    mounted () {    }  }</script><style scoped></style>----------------------------------------// 組件b<template>    <h3>msg</h3></template><script>  export default {    name: '',    data () {      return {        msg: ''      }    },    methods: {      getParams () {        // 取到路由帶過來的參數         let routerParams = this.$route.params.dataobj        // 將資料放在當前組件的資料內        this.msg = routerParams      }    },    watch: {    // 監測路由變化,只要變化了就調用擷取路由參數方法將資料存放區本組件即可      '$route': 'getParams'    }  }</script><style scoped></style>




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.