葉子路由在位置"/home"上沒有匹配到任何元素或組件
P粉190883225
P粉190883225 2023-09-15 13:44:41
0
1
534

當嘗試路由到不同的螢幕時,我一直收到錯誤訊息「在位置「/home」匹配的葉子路由沒有元素或元件。這意味著它將預設渲染一個帶有空值的 <Outlet />,導致頁面為空。」

import './App.css';
import { BrowserRouter as Router, Routes, Route } from "react-router-dom"
import React from "react";
import Home from './Screens/Home';
import Sleep from './Screens/Sleep';
import Brew from './Screens/Brew';
import Navigation from './Navigation';
import Steam from './Screens/Steam';

function App() {
  return(
    <div className="App">
      <Router>
        <Navigation/>
        <Routes>
          <Route exact path="/home" component={<Home />} />
          <Route path="/sleep" component={<Sleep />} />
          <Route path="/brew" component={<Brew />} />
          <Route path="/steam" component={<Steam />} />
        </Routes>
      </Router>
    </div>
  )
}

export default App;

Navigation.js,用於在螢幕之間進行路由的底部選項卡導航

import React from 'react';
import { Nav, NavItem } from 'reactstrap'
import { NavLink } from 'react-router-dom';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faSearch, faHome, faUserCircle } from '@fortawesome/free-solid-svg-icons';

const tabs = [{
  route: "/home",
  icon: faHome,
  label: "Home"
},{
  route: "/sleep",
  icon: faSearch,
  label: "Sleep"
},{
  route: "/brew",
  icon: faUserCircle,
  label: "Brew"
},{
  route: "/steam",
  icon: faUserCircle,
  label: "Steam"
}]

const Navigation = (props) => {
  return (
    <div>
      {/* Bottom Tab Navigator*/}
      <nav className="navbar fixed-bottom navbar-light" role="navigation">
        <Nav className="w-100">
          <div className=" d-flex flex-row justify-content-around w-100">
            {tabs.map((tab, index) => (
              <NavItem key={`tab-${index}`}>
                <NavLink to={tab.route} className="nav-link bottom-nav-link" activeClassName="active">
                  <div className="row d-flex flex-column justify-content-center align-items-center">
                    <FontAwesomeIcon size="lg" icon={tab.icon} />
                    <div className="bottom-tab-label">{tab.label}</div>
                  </div>
                </NavLink>
              </NavItem>
            ))}
          </div>
        </Nav>
      </nav>
    </div>
  )
};

export default Navigation;

我嘗試了多種解決方案,但都沒有成功。我有一種感覺,這與我混合使用了多個程式碼片段並試圖將它們組合在一起有關。

P粉190883225
P粉190883225

全部回覆(1)
P粉010967136

react-router@6的Route元件使用一個element屬性,接受一個ReactNode(例如JSX)或Component屬性,接受一個React Element。

查看Route的element/Component。

<Router>
  <Navigation />
  <Routes>
    <Route path="/home" element={<Home />}/>
    <Route path="/sleep" element={<Sleep />} />
    <Route path="/brew" element={<Brew />} />
    <Route path="/steam" element={<Steam />}/>
  </Routes>
</Router>

<Router>
  <Navigation />
  <Routes>
    <Route path="/home" Component={Home}/>
    <Route path="/sleep" Component={Sleep} />
    <Route path="/brew" Component={Brew} />
    <Route path="/steam" Component={Steam}/>
  </Routes>
</Router>

換句話說,不要使用上面的第二個範例,因為它是一種非最佳化的方法。

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!