[英]Custom Leaf tags in Vapor 4
我正在关注本教程:
https://theswiftdev.com/custom-leaf-tags-in-vapor-4/
在不清楚的情况下,我尝试将结构 PathTag 放入 configure.swift,然后放入 routes.swift。
struct PathTag: LeafTag {
static let name = "path"
func render(_ ctx: LeafContext) throws -> LeafData {
let value = ctx.request?.url.path ?? ""
return .string(value)
}
}
我正在使用 Vapor 4,我的 GET 和 POST 路由在 localhost:8080 上工作正常,但叶子是新的。 他说:“我们可以使用这个新创建的路径标签来构建一个基于当前路径的 URL,并带有一些额外的查询参数,例如:#path()?foo=bar。”
不确定他的意思,我在浏览器中尝试了以下操作:
http://localhost:8080/path?foo=bar
假设您有一个在localhost
上运行的 Vapor 应用程序,并且您设置了一个路由,例如:
http://localhost:8080/a/route/to
在为此路线呈现的.leaf
文件中,如果您输入:
<A href="#path()/somewhere">Click Me</A>
然后,当您单击链接时,它将带您到:
http://localhost:8080/a/route/to/somewhere
我将所有自定义标签保存在一个单独的.swift
文件中,并将代码注册到configure.swift
中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.