【问题标题】:Ionic tab bar changes URL but not rendered page离子标签栏更改 URL 但未呈现页面
【发布时间】:2020-08-12 05:59:04
【问题描述】:

我在 Ionic React 中有一个标签栏,它应该显示在某些页面上而不是其他页面上。我最近升级到 Ionic React 5.0.7,从那以后我的标签就停止工作了。具体来说,尽管单击选项卡会更改 URL,但它并不能可靠地更改呈现的页面,而是显示相同的页面。 (出于某种奇怪的原因,从 Profile 中单击 Discover 有效,但反之亦然。)尽管如此,我导航到的页面中的控制台消息仍然出现,即使没有任何可视组件出现。如果我刷新页面,就会呈现正确的页面。

我认为我的代码与示例 Ionic React Conference App 非常相似,所以我很困惑这里发生了什么。

它在 @ionic/react 和 @ionic/react-router 版本 0.0.10 中正常工作,但在 4.11.10、5.0.5 或 5.0.7 中不能正常工作。

TabBar.tsx:

const TabBar: React.FC<TabBarProps> = () =>
    <IonTabs>
        <IonRouterOutlet>
            <Route path="/:tab(discover)" component={Discover} exact />
            <Route path="/:tab(profile)" component={Profile} exact />
            <Route path="/Page3" component={Page3} />
        </IonRouterOutlet>

        <IonTabBar slot="bottom">
            <IonTabButton tab="discover" href="/discover">
                <IonIcon icon={search} />
                <IonLabel>Discover</IonLabel>
            </IonTabButton>
            <IonTabButton tab="profile" href="/profile">
                <IonIcon icon={person} />
                <IonLabel>Profile</IonLabel>
            </IonTabButton>
        </IonTabBar>
    </IonTabs>;

export default TabBar;

App.tsx:

const App: React.FC = () => {
    return (
        <IonApp>
                <IonReactRouter>
                    <IonRouterOutlet>
                        <Route exact path="/login" component={Login} />
                        <Route exact path="/register" component={Register} />
                        <Route path="/" component={TabBar} />
                    </IonRouterOutlet>
                </IonReactRouter>
        </IonApp>
    )
}

【问题讨论】:

  • 您是否检查了具有相同库版本的会议应用程序?
  • 会议应用使用@ionic/react 和@ionic/react-router ^5.0.5,这是我尝试过的版本之一

标签: reactjs ionic-framework ionic-react


【解决方案1】:

认为您缺少TabBar 组件的默认路径

<IonRouterOutlet>
   <Route path="/:tab(discover)" component={Discover} exact />
   <Route path="/:tab(profile)" component={Profile} exact />
   <Route path="/Page3" component={Page3} />
   <Route exact path="/" render={() => <Redirect to="/discover" />} />
</IonRouterOutlet>

【讨论】:

  • 为什么需要默认路径?在 App.tsx 中,我实际上还拥有 &lt;Route exact path="/" component={Login} /&gt; 作为 &lt;IonRouterOutlet&gt; 的第一个孩子,但这样做并没有对标签栏的功能产生影响。
  • 您尝试过还是只是在问一个问题?在应用程序级别设置默认路径以登录不应该对选项卡产生任何影响...让我知道在您尝试我的建议后会发生...您目前没有为选项卡路由设置默认路径...也就是说需要什么默认路径
猜你喜欢
  • 2018-05-20
  • 1970-01-01
  • 2021-05-02
  • 2019-03-09
  • 1970-01-01
  • 1970-01-01
  • 2020-01-31
  • 1970-01-01
  • 2020-06-04
相关资源
最近更新 更多