我正在尝试使用Link
代替锚标记。在此Link
标签中,我使用的是to
属性,但它给我错误,例如“您必须指定'to'属性”。
Nav.jsx代码
import React from "react";
import { Link, NavLink } from "react-router-dom";
const Navbar = () => {
return (
<nav className="nav-wrapper grey darken-3">
<div className="container">
<Link className="brand-logo">Sherlock Holmes</Link>
<ul className="right">
<li>
<Link to="/home">Home</Link>
</li>
<li>
<Link to="/about">About</Link>
</li>
<li>
<Link to="/contact">Contact</Link>
</li>
</ul>
</div>
</nav>
);
};
export default Navbar;
App.jsx的代码
import React, { Component } from "react";
import NavBar from "./components/Nav";
import { BrowserRouter, Route } from "react-router-dom";
import Home from "./components/Home";
import About from "./components/About";
import Contact from "./components/Contact";
class App2 extends Component {
state = {};
render() {
return (
<BrowserRouter>
<div className="App">
<NavBar />
<Route path="/home" component={Home} />
<Route path="/about" component={About} />
<Route path="/contact" component={Contact} />
</div>
</BrowserRouter>
);
}
}
export default App2;
这使我在ReactDom.render的App.jsx页面上出现错误,必须指定'to'属性。
答案 0 :(得分:0)
问题
<Link className="brand-logo">Sherlock Holmes</Link>
没有to
属性
解决方案
<Link to="/some_link" className="brand-logo">Sherlock Holmes</Link>