1.switch何时使用
通常情况,path和component是一一对应的,当组件的path被重复使用时,可能会按顺序查找,导致耗时较久。switch的作用为找到就停止,可以增加效率。
<switch>
<Route path="/about" component={About}/>
<Route path="/home" component={Home}/>
<Route path="/home" component={Test}/>
...
</switch>
2.样式丢失
出现场景:路由组件在多级列表中,如about home组件都在item目录下。
<MyNavLink to="item/home" >home</MyNavLink>
解决方式:
1. 删除样式引入中的‘.’,变成 '/Css/bootstrap.css'
2. public/index.html 中 引入样式时不写 ./ 写 %PUBLIC_URL%
(常用,但只在react中
有效果)
3. 使用HashRouter (不常用)