我坚持这件事。我想从 url 获取 url 参数 (/mypage/?title=my-title) 并将其显示在页面中。目前我在 gatsby 中尝试了仅限客户端的路由
import path from "path"
exports.onCreatePage = ({ page, actions }) => {
const { createPage } = actions
if (page.path.match(/^\/headline\/*/)) {
createPage({
path: "/headline",
matchPath: "/headline/*",
component: path.resolve("src/templates/headline.tsx"),
})
}
}
// gatsby-node.js
这是我的模板
import React from "react"
import { Router, RouteComponentProps } from "@reach/router"
type Props = RouteComponentProps<{
title: string
}>
const Test = ({ title = "Test title", location }: Props) => {
console.log(title)
console.log(location)
return <h1>Im test</h1>
}
const Headline = () => {
console.log("handle temp called")
return (
<Router>
<Test path="/compare/headline/:title" />
</Router>
)
}
export default Headline
// src/templates/headline.tsx
当我点击 /headline/?title=test-my-app 我的组件(测试)没有被路由器标签调用时,什么都没有呈现。有什么帮助吗?