咱们在开发单页面应用时,有时须要进入某个路由后基于参数从服务器获取数据,那么咱们首先要获取路由传递过来的参数,从而完成服务器请求,因此,咱们须要了解路由传参的几种方式,如下方式同 vue-router@4
。前端
除了使用
<router-link>
建立 a 标签来定义导航连接,咱们还能够借助 router 的实例方法,经过编写代码来实现。
params
传递
路径参数 用冒号
:
表示。
const routes = [ // 动态段以冒号开始 { path: 'details/:id', name: "details", component: Details }, ]
router.push()
方法的参数能够是一个字符串路径,或者一个描述地址的对象。
const Home = { template: '<div @click="toDetails">To Details</div>', metheds: { toDetails() { // 字符串路径 this.$router.push('/details/001') // 带有路径的对象 this.$router.push({path: '/details/001'}) // 命名路由,路由配置时,须要 name 字段 this.$router.push({ name: 'details', params: { id: '001' } }) } } }
注意,若是提供了 path
,params
会被忽略:vue
// `params` 不能与 `path` 一块儿使用 router.push({ path: '/details', params: { id: '001' } }) // -> /details
当一个路由被匹配时,它的
params 的值将在每一个组件中以
this.$route.params
的形式暴露出来。
const Details = { template: '<div>Details {{ $route.params.id }} </div>', created() { // 监听路由变化 this.$watch( () => this.$route.params, (toParams, previousParams) => { // 对路由变化作出响应... } ) }, }
query
传递这种状况下 query
(查询参数)传递的参数会显示在 url 后面,如:/details/001?kind=car
。vue-router
使用 query
时,如下三种方式都是可行的:编程
this.$router.push('/details/001?kind=car')
this.$router.push({ path: '/details/001', query: { kind: "car" }})
this.$router.push({ name: 'details', params: { id: '001' }, query: { kind: 'car' }})
组件经过 $route.query
获取:服务器
const Details = { template: '<div>Details {{ $route.query.kind }} </div>', created() { // 监听路由变化 this.$watch( () => this.$route.query, (toParams, previousParams) => { // 对路由变化作出响应... } ) }, }
要对同一个组件中参数的变化作出响应的话,你能够简单地 watch$route
对象上的任意属性,在这个场景中,就是$route.query
。
hash
传递经过此方式,url 路径中带有 hash
,例如:/details/001#car
。ide
使用 hash
时,如下三种方式都是可行的(同 query
):函数
this.$router.push('/details/001#car')
this.$router.push({ path: '/details/001', hash: '#car'})
this.$router.push({ name: 'details', params: { id: '001' }, hash: 'car'})
组件经过 $route.hash.slice(1)
获取:this
const Details = { template: '<div>Details {{ $route.hash.slice(1) }} </div>', }
在组件中使用$route
会与路由紧密耦合,这限制了组件的灵活性,由于它只能用于特定的 URL。虽然这不必定是件坏事,但咱们能够经过props
配置来解除这种行为。
以解耦的方式使用 props
进行参数传递,主要是在路由配置中进行操做。url
当 props
设置为 true
时,route.params
将被设置为组件的 props。spa
例以下面的代码是经过 $route
的方式获取动态字段 id
:
const User = { template: '<div>User {{ $route.params.id }}</div>' } const routes = [{ path: '/user/:id', component: User }]
将上面的代码替换成 props
的形式,以下:
const User = { props: ['id'], // 组件中经过 props 获取 id template: '<div>User {{ id }}</div>' } // 路由配置中,增长 props 字段,并将值 设置为 true const routes = [{ path: '/user/:id', component: User, props: true }]
注意:对于有命名视图的路由,你必须为每一个命名视图定义 props
配置:
const routes = [ { path: '/user/:id', components: { default: User, sidebar: Sidebar }, // 为 User 提供 props props: { default: true, sidebar: false } } ]
当 props
是一个对象时,它将原样设置为组件 props。当 props 是静态的时候颇有用。
const routes = [ { path: '/hello', component: Hello, props: { name: 'World' } } ]
const Hello = { props: { name: { type: String, default: 'Vue' } }, template: '<div> Hello {{ name }}</div>' }
<Hello />
组件默认显示 Hello Vue,但路由配置了 props
对象,当路由跳转到 /hello
时,会显示传递过来的 name
, 页面会显示为 Hello World。
能够建立一个返回 props 的函数。这容许你将参数转换为其余类型,将静态值与基于路由的值相结合等等。
使用函数模式时,返回 props 的函数接受的参数为路由记录 route
。
// 建立一个返回 props 的函数 const dynamicPropsFn = (route) => { return { name: route.query.say + "!" } } const routes = [ { path: '/hello', component: Hello, props: dynamicPropsFn } ]
当 URL 为 /hello?say=World
时, 将传递 {name: 'World!'}
做为 props 传给 Hello
组件。
const Hello = { props: { name: { type: String, default: 'Vue' } }, template: '<div> Hello {{ name }}</div>' }
此时页面将渲染:
注意:请尽量保持 props
函数为无状态的,由于它只会在路由发生变化时起做用。若是你须要状态来定义 props,请使用包装组件,这样 vue 才能够对状态变化作出反应。
1. 经过 Vuex 进行传递
1. store 存储状态; 2. A 组件更改 store 中的状态; 3. B 组件从 store 中获取。
2. 经过前端本地存储等方式
1. Local Storage; 2. Session Storage; 3. IndexedDB; 4. Web SQL; 5. Cookies。