Javascript 排除特定URL的React路由器动态URL
我想为/:user创建一个不在特定url中运行的动态url。因为/:user链接到base,所以它为我创建的每个路由运行。有没有排除特定url路由的方法?我故意不想使用像/user/:userid这样的url句柄来解决这个问题Javascript 排除特定URL的React路由器动态URL,javascript,reactjs,react-router,Javascript,Reactjs,React Router,我想为/:user创建一个不在特定url中运行的动态url。因为/:user链接到base,所以它为我创建的每个路由运行。有没有排除特定url路由的方法?我故意不想使用像/user/:userid这样的url句柄来解决这个问题 从“React”导入React; 从“react dom”导入react dom; 从“react Router dom”导入{Router,Route}; 从“历史”导入{createBrowserHistory}; 从“../src/components/provid
从“React”导入React;
从“react dom”导入react dom;
从“react Router dom”导入{Router,Route};
从“历史”导入{createBrowserHistory};
从“../src/components/provider/AuthProvider.js”导入{AuthProvider};
从“../src/views/Register”导入寄存器;
从“../src/views/Login”导入登录名;
从“../src/views/Admin”导入管理员;
从“../src/views/User”导入用户;
从“./App”导入应用程序;
导入“/assets/scss/style.scss”;
const history=createBrowserHistory();
ReactDOM.render(
,
document.getElementById('root'))
);
const User=({match,location})=>{
让load=true;
如果(location.pathname=='/admin'| | location.pathname==='/login'| | location.pathname==='/register'){
负载=假;
}
返回(
{加载&&
这是用户{match.params.user}的页面
}
)
}
导出默认用户;
您可以创建一个私有路由组件,在该组件中可以传递一个方法,以确保您已通过身份验证
非认证方法
export const isAuthenticated = () => {
if(typeof window === undefined){
return false;
}
if(localStorage.getItem("jwt")){
return JSON.parse(localStorage.getItem("jwt"));
}else{
return false;
}
}
privaterout.js
const PrivateRoute = ({component:Component, ...rest}) => {
return <Route
{...rest}
render={props=>
isAuthenticated()?(<Component {...props}/>
):(
<Redirect
to={{
pathname:"/login",
state:{ from:props.location }
}}
/>
)}
/>
}
const PrivateRoute=({component:component,…rest})=>{
返回
是否已验证()(
):(
)}
/>
}
您可以创建一个私有路由组件,在该组件中可以传递一个方法,以确保您已通过身份验证
非认证方法
export const isAuthenticated = () => {
if(typeof window === undefined){
return false;
}
if(localStorage.getItem("jwt")){
return JSON.parse(localStorage.getItem("jwt"));
}else{
return false;
}
}
privaterout.js
const PrivateRoute = ({component:Component, ...rest}) => {
return <Route
{...rest}
render={props=>
isAuthenticated()?(<Component {...props}/>
):(
<Redirect
to={{
pathname:"/login",
state:{ from:props.location }
}}
/>
)}
/>
}
const PrivateRoute=({component:component,…rest})=>{
返回
是否已验证()(
):(
)}
/>
}