Reflex框架中redirect()方法失效问题求助
Reflex框架中redirect()页面跳转失效的解决方法
你的代码问题出在状态方法没有返回redirect命令。在Reflex的状态类方法里,要触发页面跳转这类前端操作,必须将rx.redirect()的结果作为方法的返回值返回,而不是仅调用它。
修正后的代码
import reflex as rx class State(rx.State): def next(self): print("Going to next") # 关键:返回redirect命令,让Reflex执行跳转 return rx.redirect("/nextpage") def nextpage(): return rx.text("Here is the next page.", size="7") def index() -> rx.Component: return rx.container( rx.heading("Test Redirect method"), rx.text("Click on the button below to see the next page"), rx.button("Click Me", on_click=State.next), ) app = rx.App() app.add_page(index) app.add_page(nextpage, route="/nextpage")
其他可选实现方式
如果不需要在跳转前执行后端逻辑(比如你的print操作),也可以直接在按钮的on_click事件中调用rx.redirect,无需通过状态方法:
rx.button("Click Me", on_click=rx.redirect("/nextpage"))
说明
rx.redirect()方法是完全可用的,不需要额外配置。- 状态方法中所有需要触发前端变更的操作(跳转、弹窗、状态更新等),都需要以返回值的形式传递给Reflex框架执行。
内容的提问来源于stack exchange,提问作者Factor Three
相关产品推荐
相关产品推荐

