Vue 动态传值,Get传值

本文详细介绍了Vue.js中两种常见的路由传值方式:动态路由传值和GET方式传值,并提供了具体的实现步骤与示例代码。

Vue 路由get传值
1.动态传值
  1.1需要在路由配置时指定参数: {component:'/home/:id'}
  1.2在routerlink中指定格式:<router-link :to="'/home/'+123"></router-link>
  1.3在跳转到的页面中通过this.$route.params获取指定的值
2.Get传值
  2.1需要在路由配置时指定参数: {component:'/home'}
  2.2在routerlink中指定格式:<router-link :to="'/home?aid='+123"></router-link>
  2.3在跳转到的页面中通过this.$route.query获取指定的值

示例代码

import Vue from 'vue'
import App from './App.vue'

import Home from './components/Home.vue'
import Detail from './components/Detail.vue'

import VueRouter from 'vue-router'
Vue.use(VueRouter)

const routes = [{
  path: '/home',
  component: Home
}, {
  path: '/detail/:id',
  component:Detail
},{
  path:'/detailget',
  component:Detail
}, {
  path: '*',
  redirect: '/home'
}]

const router = new VueRouter({routes});

new Vue({
  el: '#app',
  router,
  render: h => h(App)
})
<template>
    <div id="home">
        我是Home
        <ul>
            <li v-for="(item,key) in list"> <router-link :to="'/detail/'+key">{{item}}</router-link> </li>
        </ul>

        <ul>
            <li v-for="(item,key) in list"> <router-link :to="'/detailget?id='+key">{{item}}</router-link> </li>
        </ul>
    </div>
</template>
<script>
export default {
  data() {
    return {
      list: ["123", "456"]
    };
  }
};
</script>
<template>
    <div id="detail"></div>
</template>
<script>
export default {
    mounted(){
        console.log("动态穿过来的值是:"+this.$route.params.id);
        console.log("Get穿过来的值是"+this.$route.query.id)
    }
}
</script>

 

转载于:https://www.cnblogs.com/chenyishi/p/9173316.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值