Vapor 4中的自定义标签

问题描述 投票:0回答:1

我正在按照这个教程进行操作。

https://theswiftdev.com/custom-leaf-tags-in-vapor-4/

在不清楚的情况下,我试着把PathTag结构放在configure.swift里,然后放在rous.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,在localhost:8080上GET和POST路由工作正常,但叶子是新的。他说:"我们可以使用这个新创建的路径标签,根据当前路径构建一个URL,并添加一些额外的查询参数,比如这样。#path()?foo=bar."

不知道他是什么意思,我在浏览器中试了下。

http://localhost:8080/path?foo=bar
swift xcode vapor leaf
1个回答
1
投票

假设你有一个运行在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.

© www.soinside.com 2019 - 2024. All rights reserved.