1

我正在关注本教程:

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
4

1 回答 1

2

假设您有一个正在运行的 Vapor 应用程序,localhost并且您设置了一条路线,例如:

http://localhost:8080/a/route/to

.leaf为此路线呈现的文件中,如果您输入:

<A href="#path()/somewhere">Click Me</A>

然后,当您单击链接时,它将带您到:

http://localhost:8080/a/route/to/somewhere

我将所有自定义标签保存在一个单独的.swift文件中,并将代码注册到configure.swift.

于 2020-06-09T15:35:45.353 回答