通过程序化导航传递道具 Vue.js
Passing props with programmatic navigation Vue.js
我有一个 Vue 组件,它有一个名为 'title' 的道具,例如:
<script>
export default {
props: ['title'],
data() {
return {
}
}
}
</script>
我在某个操作完成后以编程方式导航到组件。
有没有办法在设置道具值的同时以编程方式路由用户?我知道你可以像这样创建一个 link:
<router-link to="/foo" title="example title">link</router-link>
但是,有没有办法做类似下面的事情?
this.$router.push({ path: '/foo', title: 'test title' })
编辑:
按照建议,我已将路线更改为以下内容:
{
path: '/i/:imageID',
component: Image,
props: true
}
并导航到以下内容:
this.$router.push({ path: '/i/15', params: {title: 'test title' }})
但是,我的图像组件(模板 - 见下文)仍然没有显示任何标题。
<h1>{{ title}}</h1>
有什么可能导致问题的地方吗?
使用参数。
this.$router.push({ name: 'foo', params: {title: 'test title' }})
注意:您必须指定 name
。如果您使用 path
.
调用 this.$router.push
,这将不起作用
并设置路由以接受参数作为道具。
{path: "/foo", name:"foo", component: FooComponent, props: true}
props: true
是 documented here.
vue-router 文档明确指出 params
仅适用于 name
而不适用于 path
。
// set props: true in your route definition
const userId = 123
router.push({ name: 'user', params: { userId }}) // -> /user/123
// This will NOT work
router.push({ path: '/user', params: { userId }}) // -> /user
如果您使用 path
,请在路径本身中传递参数或使用 query
,如下所示:
router.push({ path: `/user/${userId}` }) // -> /user/123
// with query, resulting in /register?plan=private
router.push({ path: 'register', query: { plan: 'private' }})
运行 与路由器中定义的子路由存在同样的问题。 router.js 下面显示了映射到命名
的子路由
<router-view name="create"></router-view>
<router-view name="dashboard"></router-view>
router.js
{
path: "/job",
name: "Job",
component: () => import("./views/JobPage"),
children: [
{
path: "create",
name: "JobCreate",
components: {
create: JobCreate
}
},
{
path: ":id",
name: "JobConsole",
components: {
dashboard: JobConsole
}
}
]
},
当我尝试从 create 传递 props 时,vue-router 无法捕获 JobConsole 所需的动态路由匹配:
this.$router.push(
{
name: "Job",
params: {
id: this.ID_From_JobCreate
}
)
我有一个 Vue 组件,它有一个名为 'title' 的道具,例如:
<script>
export default {
props: ['title'],
data() {
return {
}
}
}
</script>
我在某个操作完成后以编程方式导航到组件。 有没有办法在设置道具值的同时以编程方式路由用户?我知道你可以像这样创建一个 link:
<router-link to="/foo" title="example title">link</router-link>
但是,有没有办法做类似下面的事情?
this.$router.push({ path: '/foo', title: 'test title' })
编辑:
按照建议,我已将路线更改为以下内容:
{
path: '/i/:imageID',
component: Image,
props: true
}
并导航到以下内容:
this.$router.push({ path: '/i/15', params: {title: 'test title' }})
但是,我的图像组件(模板 - 见下文)仍然没有显示任何标题。
<h1>{{ title}}</h1>
有什么可能导致问题的地方吗?
使用参数。
this.$router.push({ name: 'foo', params: {title: 'test title' }})
注意:您必须指定 name
。如果您使用 path
.
this.$router.push
,这将不起作用
并设置路由以接受参数作为道具。
{path: "/foo", name:"foo", component: FooComponent, props: true}
props: true
是 documented here.
vue-router 文档明确指出 params
仅适用于 name
而不适用于 path
。
// set props: true in your route definition
const userId = 123
router.push({ name: 'user', params: { userId }}) // -> /user/123
// This will NOT work
router.push({ path: '/user', params: { userId }}) // -> /user
如果您使用 path
,请在路径本身中传递参数或使用 query
,如下所示:
router.push({ path: `/user/${userId}` }) // -> /user/123
// with query, resulting in /register?plan=private
router.push({ path: 'register', query: { plan: 'private' }})
运行 与路由器中定义的子路由存在同样的问题。 router.js 下面显示了映射到命名
的子路由<router-view name="create"></router-view>
<router-view name="dashboard"></router-view>
router.js
{
path: "/job",
name: "Job",
component: () => import("./views/JobPage"),
children: [
{
path: "create",
name: "JobCreate",
components: {
create: JobCreate
}
},
{
path: ":id",
name: "JobConsole",
components: {
dashboard: JobConsole
}
}
]
},
当我尝试从 create 传递 props 时,vue-router 无法捕获 JobConsole 所需的动态路由匹配:
this.$router.push(
{
name: "Job",
params: {
id: this.ID_From_JobCreate
}
)