简体   繁体   English

如何在 InertiaJS 中传递参数?

[英]How to pass the parameter in InertiaJS?

I got a problem with how the URL appeared in the browser after I have set up a route and call it through Inertia .在设置路由并通过 Inertia 调用后,我遇到了 URL 在浏览器中如何显示的问题。

The route Route::get('/blogs/{post}', [BlogController::class, 'show']);路由Route::get('/blogs/{post}', [BlogController::class, 'show']); from web.php works fine if I input it manually, eg localhost:3000/blogs/1 .如果我手动输入它,来自web.php工作正常,例如localhost:3000/blogs/1

However, the <Link> would not work if I click the button, instead the URL is displayed as http://localhost:3000/blogs/?post=1 .但是,如果我单击该按钮, <Link>将不起作用,而是将 URL 显示为http://localhost:3000/blogs/?post=1 Is there anyway to remove the ?post= from the URL?有没有办法从 URL 中删除?post=

Below is my Vue component displaying the <Link>下面是我的 Vue 组件显示<Link>

<div class="wrapper" v-if="blogs.length">
          <div class="blog" v-for="blog in blogs" :key="blog">
            <Link href="/blogs/" :data="{post:blog.id}">
              <small>posted by: {{ blog.id }}</small>
              {{ blog.title }}
            </Link>
            <button type="button" @click="destroy($event, blog.id)">
                Delete post
            </button>
          </div>
</div>

Note that I am following the docs from https://inertiajs.com/links .请注意,我正在关注https://inertiajs.com/links 中的文档。

The problem is that you're passing the data prop.问题是您正在传递data道具。 This will cause the object to be passed as the payload of the request.这将导致对象作为请求的有效负载传递。 That's why it is ending up appended to the route as a query string.这就是为什么它最终作为查询字符串附加到路由中的原因。

// change this
<Link href="/blogs/" :data="{post:blog.id}">

// to this
<Link href="/blogs/your-blog-id-goes-here" >

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM