Vue2路由中的参数传递

params传参

​this.$router.push({name: 'index'}, params: {id: item.id})

​this.$router.params.id

路由属性传递

​this.$router.push({name: '/index/${item.id}'})

// ​路由配置 {path: '/index/:id'}

query传参(可以解决页面刷新参数的丢失问题)

this.$router.push({

​	name: 'index',

​	query: {id: item.id}

​})

Vue2中的路由配置以及demo

router/index.js

import VueRouter from 'vue-router'
import Vue from 'vue'

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        // name: '/',
        component: () => import('@/view/test.vue')
    },
    {
        path: '/test',
        name: 'test',
        component: () => import('@/view/test-b.vue')
    },
    {
        path: '/test-c/:id',
        name: 'test-c',
        component: () => import('@/view/test-c.vue')
    },
    {
        path: '/test-d',
        name: 'test-d',
        component: () => import('@/view/test-d.vue')
    },
]

const router = new VueRouter({
    routes
})

export default router

main.js

import Vue from 'vue'
import App from './App.vue'
import router from '@/router/index'
import VueRouter from 'vue-router'

Vue.config.productionTip = false
Vue.use(VueRouter)

new Vue({
  router,
  render: h => h(App),
}).$mount('#app')

app.vue

<template>
  <div id="app">
    <router-view></router-view>
  </div>
</template>

<script>

export default {
  name: 'App',
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

test.vue

<template>
    <div class="test">
        测试A
        <button @click="changeUrl('test')">跳转页面至testB</button>
        <button @click="changeUrl('test-c')">跳转页面至testC</button>
        <button @click="changeUrl('test-d')">跳转页面至testD</button>
    </div>
</template>

<script>
export default {
    name: 'test-a',
    methods: {
        changeUrl(name) {
            if(name == 'test') {
                this.$router.push({name, params: {id: "12345"}})
            } else if(name == 'test-c') {
                this.$router.push(`/${name}/123456`)
            } else {
                this.$router.push({name, query: {id: "12345"}})
            }
        }
    }
}
</script>

<style scoped>

</style>

test-b.vue

<template>
    <div class="test">
        <p>测试B</p>
        <p>{{ msg }}</p>
        <button @click="changeUrl">跳转页面至testA</button>
    </div>
</template>

<script>
export default {
    name: 'test-b',
    data() {
        return {
            msg: ""
        }
    },
    mounted() {
        this.msg = this.$route.params.id       
    },
    methods: {
        changeUrl() {
            this.$router.push('/')
        }
    },
}
</script>

<style scoped>

</style>

test-c.vue

<template>
    <div class="test">
        <p>测试C</p>
        <p>{{ msg }}</p>
        <button @click="changeUrl">跳转页面至testA</button>
    </div>
</template>

<script>
export default {
    name: 'test-c',
    data() {
        return {
            msg: ""
        }
    },
    mounted() {
        this.msg = this.$route.params.id       
    },
    methods: {
        changeUrl() {
            this.$router.push('/')
        }
    },
}
</script>

<style scoped>

</style>

test-d.vue

<template>
    <div class="test">
        <p>测试D</p>
        <p>{{ msg }}</p>
        <button @click="changeUrl">跳转页面至testA</button>
    </div>
</template>

<script>
export default {
    name: 'test-d',
    data() {
        return {
            msg: ""
        }
    },
    mounted() {
        this.msg = this.$route.query.id       
    },
    methods: {
        changeUrl() {
            this.$router.push('/')
        }
    },
}
</script>

<style scoped>

</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值