【发布时间】:2021-10-03 19:43:31
【问题描述】:
我有一个使用 react 制作的网络应用程序,该应用程序通过 firebase 托管托管,还有一个使用 express 托管在云功能上的后端服务器。
登陆、登录和注册页面按预期工作,但是在任何带有 /app/{route} 的路由(例如 /app/dashboard)上,都会返回一个空白页面并且请求返回“您需要启用 javascript 才能运行这个应用”
这里的一切都在开发服务器上运行,react 网站通过 npm start 启动,并通过 firebase 与云功能通信。
知道为什么吗?
//routes.js
const Dashboard = lazy(() => import('../pages/Dashboard'))
const Tasks = lazy(() => import('../pages/Tasks'))
const routes = [
{
path: '/dashboard', // the url
component: Dashboard, // view rendered
},
{
path: '/tasks',
component: Tasks,
},
]
//layout.js
<Switch>
{routes.map((route, i) => {
return route.component ? (
<Route
key={i}
exact={true}
path={`/app${route.path}`}
render={(props) => <route.component {...props} />}
/>
) : null
})}
<Route component={Page404} />
</Switch>
//app.js
export const AuthenticatedRoute = ({ component: C, ...props }) => {
const { user } = useAuth() //useAuth is linked to firebase authStateObserver
return (
<Route
{...props}
render={routeProps =>
user ? <C {...routeProps} /> : <Redirect excat to="/login" />
}
/>
)
}
const UnauthenticatedRoute = ({ component: C, ...props }) => {
const { user } = useAuth()
return (
<Route
{...props}
render={routeProps =>
!user ? <C {...routeProps} /> : <Redirect excat to="/app/dashboard" />
}
/>
)
}
<UnauthenticatedRoute excat path="/create-account" component={CreateAccount} />
<AuthenticatedRoute path="/app" component={Layout} />
<Route excat path="/" component={LandingPage} />
我是网络开发的新手,如果我能提供其他任何东西,请告诉我。
【问题讨论】:
-
“知道为什么吗?” - 没有看到任何代码...没有
-
为什么只路由/app/{route}?移动到 /app2/{route} 怎么样
-
添加了相关代码@Bravo的sn-ps
-
您在预览中看到的是 React 的 index.html 页面。这是正常的——即使对于正在工作的页面也是如此。您在浏览器页面中看到一个空白页面而不是“您需要启用 Javascript 才能运行此应用程序”这一事实意味着 javascript 已启用并且正在工作 - React 的 javascript 代码删除了“您需要...”消息 - 所以页面为空白。您的错误是组件未呈现。停止查看您的网络选项卡(这是正常的)。查看您的控制台以获取错误消息。
标签: javascript reactjs firebase express web