Golang与React Router一起使用吗?

huangapple go评论179阅读模式
英文:

Golang with React Router?

问题

如何在Go中使用React Router?

例如,我在Go中有以下代码:

Echo.Get("/*", *handler which return page with ReactRouter*)

而在React Router中有以下代码:

React.render((
    <Router history={History.createHistory()}>
        <Route name="index" path="/" component={Main}>
            <Route name="users" path="/users" component={Users}>
                <Route name="user" path="/user/:userId" component={User} />
            </Route>
            <Route path="*" component={NotFound} />
        </Route>
    </Router>
), document.body)

但是我一直得到的是主页(/)页面。所有的路由都表现得一样('/users','/user/qwe'等)。

希望这对你有帮助!

英文:

How do I use React Router with Go?


For example,

I have this in Go:

Echo.Get(&quot;/*&quot;, *handler which return page with ReactRouter*)

And this in React Router:

React.render((
&lt;Router history={History.createHistory()}&gt;
	&lt;Route name=&quot;index&quot; path=&quot;/&quot; component={Main}&gt;
		&lt;Route name=&quot;users&quot; path=&quot;/users&quot; component={Users}&gt;
			&lt;Route name=&quot;user&quot; path=&quot;/user/:userId&quot; component={User} /&gt;
		&lt;/Route&gt;
		&lt;Route path=&quot;*&quot; component={NotFound} /&gt;
	&lt;/Route&gt;
&lt;/Router&gt;
), document.body)

But I keep getting the index (/) page. All routes behave the same ('/', '/users', '/user/qwe', etc.)

答案1

得分: 2

你需要使用新的绑定来渲染不同的路由,或者在处理函数中专门处理该绑定,并读取请求对象的信息。

Echo.Get("/user/:id", getUser)
Echo.Get("/users", listUsers)
Echo.Get("/*", catchAllRemainingCalls)

希望这对你有帮助。祝你好运!

英文:

You have to render different routes with new bindings, or handle that binding specifically in your handler function and read out the request object's information.

Echo.Get(&quot;/user/:id&quot;, getUser)
Echo.Get(&quot;/users&quot;, listUsers)
Echo.Get(&quot;/*&quot;, catchAllRemainingCalls)

Hope this helps. Good luck!

huangapple
  • 本文由 发表于 2015年9月14日 06:10:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/32555038.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定