- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在制作一个食物应用程序,当每个组件上的按钮是它时
在另一个页面上打开并且 URL 也发生了变化。所以我正在使用 react 路由器但是当我单击按钮时它不会打开该组件
这是 App.js
import React from "react";
import "bootstrap/dist/css/bootstrap.min.css";
import "./App.css";
import {
BrowserRouter as Router,
Switch,
Route,
Link
} from "react-router-dom";
import About from "./About"
import Contact from "./Contact"
import Home from "./Home"
import Menu from "./Menu"
function App() {
return (
<div>
<div>
<div className="navbar">
<h3>Food Festive</h3>
<div>
<ul>
<li>
<Link className="chg" to="/home">Home</Link>
</li>
<li>
<Link className="chg" to="/about">About</Link>
</li>
<li>
<Link className="chg" to="/contact">Contact</Link>
</li>
<li>
<Link className="chg" to="/menu">Menu</Link>
</li>
</ul>
</div>
</div>
<Switch>
<Router path="/home">
<Home />
</Router>
<Router path="/about">
<About />
</Router>
<Router path="/contact">
<Contact />
</Router>
<Router path="/menu">
<Menu />
</Router>
</Switch>
</div>
</div>
);
}
export default App;
这里的 CardItems.js 我希望当我单击 Button 时它会单独打开 ButtonClick.js。但是当我单击它时,它会显示在这样的按钮之后:
import React from "react";
import { Card, Button, CardDeck } from "react-bootstrap";
import "bootstrap/dist/css/bootstrap.min.css";
import "./App.css";
import ButtonClick from "./ButtonClick"
import {
BrowserRouter as Router,
Switch,
Route,
Link
} from "react-router-dom";
function CardItems(props) {
function name() {
console.log(`${props.item.name}`)
}
return (
<div className="wrap">
<Card>
<Card.Img variant="top" src={require(`${props.item.img}`)} />
<Card.Body>
<h5>{props.item.name}</h5>
<div>
<h6>Price : ${props.item.price} </h6>
<h6>Orders till now : {props.item.Orders} </h6>
<h6>Home Delivery Time : {props.item.Delivery} </h6>
<h6>Quantity : {props.item.Quantity}</h6>
</div>
<Link to="/newpage">
<Button variant="info" onClick={name}>
Details
</Button>
</Link>
<Switch>
<Router path="/newpage">
<ButtonClick />
</Router>
</Switch>
</Card.Body>
</Card >
</div>
);
}
export default CardItems;
CardItems 数组
const CardItemsArray = [
{
id: 1,
img: "./images/Food/cupcakes.jpg",
name: "Cupcakes",
price: 20,
Orders: 3025,
Delivery: "45mins",
Quantity: "6 Cup-cakes",
},
{
id: 2,
img: "./images/Food/fried-chicken.jpg",
name: "Fried Chicken",
price: 30,
Orders: 3001,
Delivery: "45mins",
Quantity: "3 Pieces",
},
{
id: 3,
img: "./images/Food/pizza23.jpg",
name: "Pizza",
price: 50,
Orders: 5029,
Delivery: "45mins",
Quantity: "1 Pizza",
},
{
id: 4,
img: "./images/Food/instant-nood.jpg",
name: "Instant Noodles",
price: 10,
Orders: 1042,
Delivery: "45mins",
Quantity: "1 Plate",
},
{
id: 5,
img: "./images/Food/hamburger.jpg",
name: "Hamburger",
price: 40,
Orders: 2134,
Delivery: "45mins",
Quantity: "1 Burgur",
},
{
id: 6,
img: "./images/Food/sushi.jpg",
name: "Shushi",
price: 100,
Orders: 1872,
Delivery: "45mins",
Quantity: "1 Plate",
},
{
id: 7,
img: "./images/Food/ice.jpg",
name: "Ice-Cream",
price: 10,
Orders: 1272,
Delivery: "45mins",
Quantity: "1 Cup",
},
{
id: 8,
img: "./images/Food/chicken-stack.jpg",
name: "Beef Steack",
price: 70,
Orders: 2381,
Delivery: "45mins",
Quantity: "1 Piece",
},
{
id: 9,
img: "./images/Food/fries.jpg",
name: "Fries",
price: 15,
Orders: 4231,
Delivery: "45mins",
Quantity: "1 Pack",
},
{
id: 10,
img: "./images/Food/ice-cups.jpg",
name: "Ice-Cream-Cups",
price: 20,
Orders: 2836,
Delivery: "45mins",
Quantity: "2 Cups",
},
{
id: 11,
img: "./images/Food/strawb-smo.jpg",
name: "Strawberry Smoothie",
price: 30,
Orders: 2981,
Delivery: "45mins",
Quantity: "1 Glass",
},
{
id: 12,
img: "./images/Food/salad2.jpg",
name: "Salad",
price: 20,
Orders: 2311,
Delivery: "45mins",
Quantity: "1 Plate",
},
{
id: 13,
img: "./images/Food/orange juice.jpg",
name: "Orange Juice",
price: 15,
Orders: 1637,
Delivery: "45mins",
Quantity: "1 Glass",
},
{
id: 14,
img: "./images/Food/fruits-beauti.jpg",
name: "Fruit Salad",
price: 30,
Orders: 1091,
Delivery: "45mins",
Quantity: "1 Plate",
},
{
id: 15,
img: "./images/Food/tea1.jpg",
name: "Ice Tea",
price: 20,
Orders: 2348,
Delivery: "45mins",
Quantity: "1 Cup",
},
{
id: 16,
img: "./images/Food/tea2.jpg",
name: "Mint Tea",
price: 20,
Orders: 2536,
Delivery: "45mins",
Quantity: "1 Cup",
},
{
id: 17,
img: "./images/Food/bread.jpg",
name: "Bread Cuisine",
price: 40,
Orders: 975,
Delivery: "45 mins",
Quantity: "1 Plate",
},
{
id: 18,
img: "./images/Food/lime-juice.jpg",
name: "Lime Juice",
price: 20,
Orders: 6437,
Delivery: "45 mins",
Quantity: "1 Glass",
},
{
id: 19,
img: "./images/Food/pizza2.jpg",
name: "Fagita Pizza",
price: 100,
Orders: 1862,
Delivery: "45 mins",
Quantity: "1 Pizza",
},
{
id: 20,
img: "./images/Food/cake2.jpg",
name: "Cake",
price: 150,
Orders: 2154,
Delivery: "45 mins",
Quantity: "1 Cake",
},
];
export default CardItemsArray;
菜单.js
import React from "react";
import CardItems from "./CardItems.js";
import CardItemsArray from "./CardItemsArray";
function Menu() {
const items = CardItemsArray.map((item) => (
<CardItems key={item.id} item={item} />
));
return (
<div>
<div>{items}</div>
</div>
);
}
export default Menu;
按钮点击.js
import React from "react";
function ButtonClick(props) {
return (
<div>
<h1>Button is Clicked</h1>
</div>
);
}
export default ButtonClick;
最佳答案
我认为您一直在以错误的方式使用 React Router。
以下是开始使用 React Router 的 3 个技巧:
<Route path="/thePath" component={ComponentName} />
<Link to="/thePath"><button>Go to a certain route</button></Link>
import React from "react";
import "bootstrap/dist/css/bootstrap.min.css";
import "./App.css";
import {
BrowserRouter as Router,
Switch,
Route,
Link
} from "react-router-dom";
import About from "./About"
import Contact from "./Contact"
import Home from "./Home"
import Menu from "./Menu"
import ButtonClick from "./ButtonClick"
function App() {
return (
// HERE, add Router to the top level of your app.
// Refer to tip number 1.
<Router>
<div>
<div className="navbar">
<h3>Food Festive</h3>
<div>
<ul>
<li>
<Link className="chg" to="/home">Home</Link>
</li>
<li>
<Link className="chg" to="/about">About</Link>
</li>
<li>
<Link className="chg" to="/contact">Contact</Link>
</li>
<li>
<Link className="chg" to="/menu">Menu</Link>
</li>
</ul>
</div>
</div>
// HERE, don't use Router, but use Route.
// Refer to tip number 2.
<Route path="/home" component={Home} />
<Route path="/about" component={About} />
<Route path="/contact" component={Contact} />
<Route path="/menu" component={Menu} />
// HERE, add this line below. Refer to tip number 2.
// You should add any routes available in your app using the Route component.
// With this code you are saying:
// If the path is equal to /button-click, please render the ButtonClick component.
<Route path="/button-click" component={ButtonClick} />
</div>
</Router>
);
}
export default App;
在你的 CardItems.js 中(其实我只是猜这个文件的名字)
import React from "react";
import { Card, Button, CardDeck } from "react-bootstrap";
import "bootstrap/dist/css/bootstrap.min.css";
import "./App.css";
import ButtonClick from "./ButtonClick"
import {
BrowserRouter as Router,
Switch,
Route,
Link
} from "react-router-dom";
function CardItems(props) {
function name() {
console.log(`${props.item.name}`)
}
return (
<div className="wrap">
<Card>
<Card.Img variant="top" src={require(`${props.item.img}`)} />
<Card.Body>
<h5>{props.item.name}</h5>
<div>
<h6>Price : ${props.item.price} </h6>
<h6>Orders till now : {props.item.Orders} </h6>
<h6>Home Delivery Time : {props.item.Delivery} </h6>
<h6>Quantity : {props.item.Quantity}</h6>
</div>
// HERE, make sure your button is wrapped with Link component, supply it with its corresponding "to" props.
// Refer to tip number 3.
// With this code, you are saying:
// If anyone clicks this button, redirects them into /button-click route.
// And because we have set this route in app.js, the user will see the ButtonClick component.
<Link to="/button-click">
<Button variant="info" onClick={name}>
Details
</Button>
</Link>
// And over here, you don't need any Switch.
</Card.Body>
</Card >
</div>
);
}
export default CardItems;
而已!希望能帮助到你。如果您仍然遇到错误,请告诉我。
关于javascript - react 路由器在按钮单击时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62942445/
使用 ui-router-ng2 有哪些优点和缺点?而不是新的 Angular2 路由器?过去我使用 ui-router 和 Angular 1.x 而不是使用 ngRoute,因为我需要更好地支持嵌
本文主要记录个人对小米 WR30U 路由器的解锁和刷机过程,整体步骤与 一般安装流程 类似,但是由于 WR30U 的解锁 ssh 和刷机的过程中有一些细节需要注意,因此记录一下 解锁
我有路由器 { path: 'reset-password', component: ResetPasswordComponent, } 我希望能够将其作为直接链接 (/reset-passw
所以,我试图制作一个具有 Backbone 的示例应用程序,但我无法使路由器工作,对此有什么想法吗?谢谢。这是我的路由器 var Workspace = Backbone.Router.extend(
我正在研究分布式应用程序。网络基于ZMQ(jeromq),这是我的架构: N Clients (Dealer socket) (Router) Proxy (Dealer) 1 (Dealer)
In this plunk我有一个 ui-router $state.goto这仅在第一次有效,即使我增加了 seq变量以强制重新加载。看到时间戳没有改变。这段代码有什么问题? HTML G
我有面包屑组件,测试如下。测试应覆盖75%以上。测试失败,因为 ActivatedRoute.root.children 未定义。谁知道我该如何定义它?我需要至少一个 children 作为 root
我正在使用 RC5 和 "@angular/router": "3.0.0-rc.1" 我似乎无法捕捉到旧路线(示例): /category/subcategory/subcategory/artic
我确实有一个延迟加载模块,当我转到 /lazy 时默认加载 LazyLoadedComponent。到目前为止,没问题。 此组件包含 2 个路由器 socket : 模块有以下路由: const
我试图用不同的参数路由到当前页面,但没有成功。我有一个触发的组合框: this.router.navigate(['page', selectedId]); Url 正在更改,仅此而已。 如何使用不同
我正在尝试使用 React 路由器构建 SPA。当我使用链接导航时,它工作得很好,但是刷新或写入 url 时,它给我 cannot GET/(any route except main)。我试图将 h
我无法在我的 app.js 文件中添加样式。 div 在路线内不起作用。有没有其他方法可以在路由中添加样式? : } /> } /> }> } />
根据用户角色在 react-router 中有条件地呈现路由的最佳方法是什么。我有一个案例,并非所有角色都有权查看某些路线。而且我还需要处理子路线。因此,如果主要路线之一类似于/posts,我只希望
我想使用 Flash 的 RTMFP 对等协议(protocol),但我想知道它是否值得为主流受众开发?据我了解,它使用 UDP,除非用户的防火墙/路由器配置正确,否则它变得毫无用处。 这个问题有解决
我有一 strip 有 waitOn 的路线返回 Meteor.subscribe 的钩子(Hook).每次触发路线时,我都会从我的 loadingTemplate 中看到微调器在看到实际数据之前简要
所以我最近开始尝试使用 createMemoryHistory,因为我正在构建一个将在 iframe 中呈现的应用程序。幸运的是,单击 UI 按钮,我可以在不修改 url 的情况下四处导航。但是,当我
我在我的 React redux 应用程序中使用 connected-react-router。我需要服务器端渲染和客户端渲染(我通过 limenius react bundle 在 symfony
我是一名 ios 开发人员,并试图找出 viper 架构中的路由器和 coordinator 之间的区别。 我真的找不到明确的答案是协调器只是被认为是一种模式,所以路由器毕竟只是某种协调器还是它们有不
我想要这样的东西: if ($state.get("^")) $state.go("^"); else $location.path('/'); 但它显然不起作用(即使没有父状态,它也总是与 $sta
当我从 react-router-dom 使用 BrowserRouter 时,我的路由正在工作。但是为了使用自定义历史记录,我用来自 react-router 的 Router 替换了 Browse
我是一名优秀的程序员,十分优秀!