在路由中传递参数可以使用以下方法:

  1. 使用params

在路由路径中使用:来定义参数,例如:

const routes = [
  { path: '/user/:id', component: User }
]

在组件中可以通过$route.params来获取参数,例如:

const User = {
  template: '<div>User {{ $route.params.id }}</div>'
}
  1. 使用query

在路由路径中使用?来定义查询参数,例如:

const routes = [
  { path: '/user', component: User }
]

在组件中可以通过$route.query来获取参数,例如:

const User = {
  template: '<div>User {{ $route.query.id }}</div>'
}
  1. 使用props

在路由中使用props来传递参数,例如:

const routes = [
  { path: '/user/:id', component: User, props: true }
]

在组件中需要定义props来接收参数,例如:

const User = {
  props: ['id'],
  template: '<div>User {{ id }}</div>'
}

在使用路由跳转时,可以使用以下方法传递参数:

// 使用params
this.$router.push({ path: '/user/123' })

// 使用query
this.$router.push({ path: '/user', query: { id: '123' } })

// 使用props
this.$router.push({ path: '/user/123', props: true })
如何将路由里带着参数传到下一页

原文地址: https://www.cveoy.top/t/topic/bJoa 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录