Ionic 离子路由器:子页面仅在刷新页面后加载

yiytaume  于 2023-09-28  发布在  Ionic
关注(0)|答案(2)|浏览(151)

我有一个列表页面,其中显示了我的games.json文件中的所有游戏。
下面是GameListPage.tsx中的内容:

<IonList>
  {gamesJson.map((game) => (
    <IonItem
      routerLink={
        game.software_used ?
          /games/id/${game.steam_appid}` : '/games'
      }
      key={game.steam_appid}
    >
      <IonThumbnail slot="start">
        <img alt={game.name} src={game.header_image} />
      </IonThumbnail>
      <IonLabel>
        <h2>{game.name}</h2>
        <p>{game.developer}</p>
      </IonLabel>
    </IonItem>
  ))}
</IonList>

当您点击该项目时,您将被重定向到游戏详细信息页面。但是,只有在刷新页面后才会重定向。
下面是我的IonReactRouter在App.tsx中的样子:

<IonReactRouter>
  <IonTabs>
    <IonRouterOutlet>
      <Route exact path="/games">
        <GameListPage />
      </Route>
      <Route path="/games/id/:id">
        <GameDetailsPage />
      </Route>
      <Route exact path="/software">
        <SoftwareListPage />
      </Route>
      <Route exact path="/">
        <Redirect to="/games" />
      </Route>
    </IonRouterOutlet>
    <IonTabBar slot="bottom">
      <IonTabButton tab="games" href="/games">
        <IonIcon aria-hidden="true" icon={triangle} />
        <IonLabel>Games</IonLabel>
      </IonTabButton>
      <IonTabButton tab="software" href="/software">
        <IonIcon aria-hidden="true" icon={ellipse} />
        <IonLabel>Software</IonLabel>
      </IonTabButton>
    </IonTabBar>
  </IonTabs>
</IonReactRouter>

我如何解决这个问题,以便在单击列表中的项目后立即加载页面?

lb3vh1jj

lb3vh1jj1#

1.我认为你需要使用react-router-domnpm library
1.然后你需要使用<Link>组件和(to)属性<Link to={_link distination_}>{children}</Link>
例如,它将是这样的(确实在你设置好路由器之后)

import {Link} from 'react-router-dom'
function MyComponent({game}){

return 

  <IonList>
    {gamesJson.map((game) => {
      const href = game.software_used ?`/games/id/${game.steam_appid}` : '/games'
  
      return 
       <Link to={href} key={game.steam_appid}>
          <IonItem>
             <IonThumbnail slot="start">
                 <img alt={game.name} src={game.header_image}/>
             </IonThumbnail>
             <IonLabel>
                 <h2>{game.name}</h2>
                 <p>{game.developer}</p>
             </IonLabel>
         </IonItem>      
      </Link>
  })}
                        
                   
</IonList>
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

这样,您将使用react路由,页面将不会重新呈现。

esbemjvw

esbemjvw2#

值得一提的是,我相信我曾经通过将路由呈现到从react-router-dom导出的Switch组件中来解决过类似的问题。IonContent组件在您的情况下可能需要,也可能不需要,但在这里将其作为示例:

import { Redirect, Route, Switch } from "react-router-dom";
import {
  IonContent,
  IonRouterOutlet,
  IonIcon,
  IonLabel,
  IonTabs,
  IonTabBar,
  IonTabButton,
  setupIonicReact,
} from "@ionic/react";
import { IonReactRouter } from "@ionic/react-router";
<IonReactRouter>
  <IonTabs>
    <IonRouterOutlet>
      <IonContent>
        <Switch>
          <Route path="/games/id/:id" component={GameDetailsPage}  />
          <Route path="/games" component={GameListPage} />
          <Route path="/software" component={SoftwareListPage} />
          <Redirect to="/games" />
        </Switch>
      </IonContent>
    </IonRouterOutlet>
    <IonTabBar slot="bottom">
      <IonTabButton tab="games" href="/games">
        <IonIcon aria-hidden="true" icon={triangle} />
        <IonLabel>Games</IonLabel>
      </IonTabButton>
      <IonTabButton tab="software" href="/software">
        <IonIcon aria-hidden="true" icon={ellipse} />
        <IonLabel>Software</IonLabel>
      </IonTabButton>
    </IonTabBar>
  </IonTabs>
</IonReactRouter>

相关问题