[英]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.