Vue之Router路由传参之一query(043)

本文通过一个简单的Vue应用实例,介绍了如何使用Vue Router进行动态路由参数的传递。在App.vue组件中,利用`:to`绑定query参数,将news列表中的id、title和detail传给news.vue组件展示。在news.vue中,通过`$route.query`获取并显示这些参数。此教程适用于初学者掌握Vue应用中的路由传参技巧。
摘要由CSDN通过智能技术生成

Vue之Router路由传参之一query

一、目录结构
在这里插入图片描述
注:pages文件夹里为路由组件,route文件夹里为路由配置
二、main.js

import Vue from 'vue'
import App from './App.vue'
import VueRouter from 'vue-router'
import router from './router'
Vue.use(VueRouter)
Vue.config.productionTip = false

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

三、index.js

import VueRourter from 'vue-router'
import news from '../pages/news.vue'

export default new VueRourter({
    routes:[{
             path:'/news',
             component:news
    }]
})

四、App.vue

<template>
  <div id="app">
   <li v-for="n in news" :key="n.id">
     <router-link :to="{
        path:'/news',
        query:{
          id:n.id,
          title:n.name,
          detail:n.detail
        }
     }" >
       {{n.title}}
       </router-link>
   </li>
    <br>
    <div>
      <h1>展示区</h1>
      <router-view></router-view>
    </div>
  </div>
</template>

<script>
export default {
  name: 'App',
 data(){
   return{ news:[
     {id:'001',title:'life',detail:'I love life'},
     {id:'002',title:'sport',detail:'runing '},
     {id:'003',title:'sing',detail:'singing...'}
   ]}
 }
}
</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>

五、news.vue

<template>
  <div>
      <h3>新闻细节</h3>
      <li>title:{{$route.query.id}}</li>
      <li>title:{{$route.query.title}}</li>
      <li>detail:{{$route.query.detail}}</li>
   </div>
</template>

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

六、总结
在中 :to绑定quuery项。然后在路由组件里用$route.query.属性接收。

七、效果
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值