小编Jul*_*rra的帖子

React Router - 如何将先前的位置路径名传递给当前

是否可以将之前的路径传递到当前位置?

例如,我有 2 条路线。Home / Profile /profile

如果我在Profile路线中并且转到Home路线,现在我在主路线内,如何访问之前的路径名/profile

就像是:

const Home = (props) => {
   console.log(props.location.state.previousPath) // How to access previous path? 
}
Run Code Online (Sandbox Code Playgroud)

这就是我使用自定义路由构建路由器的方式

const App = () => (
  <Router history={history}>
    <main className="min-h-screen">
      <NavBar />
      <Switch>
         <PublicRoute path={ROUTE.REGISTER} component={Register} />
         <PublicRoute path={ROUTE.LOGIN} component={Login} />
         <ProtectedRoute path={ROUTE.HOME} exact component={Home} />
         <ProtectedRoute path={ROUTE.PROFILE} component={Profile} />
         <Route component={PageNotFound} />
       </Switch>
     </main>
   </Router>
)
Run Code Online (Sandbox Code Playgroud)

受保护的路由.js

const ProtectedRoute = ({ isAuth, component: Component, path, ...rest }) …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs react-router-dom

4
推荐指数
1
解决办法
2万
查看次数

NextJS 与 TypesScript index.ts 文件未使用路径别名解析

我收到一条 TypeScript 警告,指出无法找到该模块。

我在我的 NextJS 应用程序上使用 TypeScript,这只是在我使用自定义路径并且之前没有遇到过这个问题的时候发生的。

我有这个结构示例

/models
  |-- index.ts
  |-- User.ts
/pages
   /api
     /users
       |-- index.ts
Run Code Online (Sandbox Code Playgroud)

我正在尝试Userschemamodels我设置的自定义路径导入,但它给了我一个错误

这就是我的index.ts样子models

export { default as User } from './User';
Run Code Online (Sandbox Code Playgroud)

这就是我将其导入内部的方式api/user/index.ts

import { User } from '@/models';
Run Code Online (Sandbox Code Playgroud)

我的tsconfig文件看起来像这样

{
    "compilerOptions": {
        "target": "es5",
        "lib": [
            "dom",
            "dom.iterable",
            "esnext"
        ],
        "allowJs": true,
        "skipLibCheck": true,
        "strict": false,
        "forceConsistentCasingInFileNames": true,
        "noEmit": true,
        "esModuleInterop": true,
        "module": "esnext",
        "moduleResolution": "node",
        "resolveJsonModule": true,
        "isolatedModules": true,
        "jsx": "preserve",
        "baseUrl": …
Run Code Online (Sandbox Code Playgroud)

typescript next.js

2
推荐指数
1
解决办法
6438
查看次数