咱们知道react-router v4的路由嵌套并非根据Route的嵌套关系来实现的,通常实现有两种方法,一种是经过在父组件写子路由,这种方法使路由比较分散,并且子路由必须包含根路径。可是我在项目中用这种方式实现嵌套路由发现子组件不能加载成功,即页面没有显示,但也不会报错。后来改用第二种实现方式,发现能够。这是为何呢,尚未找出来~javascript
//route.js
class Routes extends React.Component{
render(){
return(
<BrowserRouter> <div> <Switch> <Route path='/' render={({history,location,match})=>( <Home history={history} location={location} match={match}> <Route exact path='/' component={Note_List} ></Route> <Route exact path='/note/:noteid' component={articleDetail} ></Route> <Route exact path='/home' component={Note_List} ></Route> <Route exact path='/user/note' component={articleEdit }></Route> <Route exact path='/user/notes' component={articleList } ></Route> <Route exact path='/about' component={About}></Route> </Home> )} /> </Switch> </div> </BrowserRouter> )} } 复制代码
//home.js
class Home extends React.Component{
render(){
return(
<div className='container'> <div> {this.props.children} </div> </div>
)
}
}
export default Home
复制代码
我使用的代码是this.props.actionNotes.findOneNote(this.props.match.params.noteid);
,实际上浏览器提示的是“noteid找不到”,而后经过加上console.log(this.props.params);
发现其实是this.props.params
未定义,而后通加上console.log(this.props);
发现要获取的noteid
属性是在this.props.match.params
里,将this.props.params.noteid
改为this.props.match.params.noteid
便可前端
在stackoverflow可找到相关答案java
这个问题困扰了我好长时间,因为是在升级react和react-dom版本以后才出现这个问题的,我就觉得是react版本的问题,而后就下降react的版本,试了好几个版本后发现问题依旧这样,甚至还出现了新的错误提示。而后咨询了一下发现有多是代码中有数组未定义,而后就想调试下找到错误代码。因为个人项目只支持打包,即npm run build
,而后我就将代码放在create-react-app
环境下,让它支持npm start
,最后经过执行npm start
就发现了代码里面其实还存在好多问题,而这些问题npm run build
是不能发现的,例如react-router中没有IndexLink了,可是项目中依然使用了indexLink,还有不少问题,而这些问题竟然都没有被发现,因此之后前端代码仍是好好放在完整的脚手架环境吧,不能直接build以后传给服务端就好了,仍是要经过npm start来调试,npm start会发现更多的问题。多么痛的领悟~~~!react
<form action="" method="post" id="LoginForm">
<input type="text" name="username" value="" />
<input type="password" name="password" value="" />
<input type="submit" id="submit" name="submit" value="Login" />
<input type="reset" id="reset" name="reset" value="Reset" />
</form>
复制代码
JS方法:document.getElementById('LoginForm').reset()
或者使用jquery:````('#LoginForm').trigger("reset")`jquery
但此时会出现上面的提示,问题在于id="reset"
和name="reset"
,这里的reset
属性覆盖了原来的reset
方法,天然没法调用并提示is not a function
,解决的办法也很简单,避免用reset
关键词来命名reset
按钮的name
和id
属性。好比下面的命名方式则比较保险:ios
<input type="reset" id="ResetButton" name="ResetButton" value="Reset" />
复制代码
从 React v15.5 开始 ,
React.PropTypes
助手函数已被弃用,咱们建议使用prop-types
库 来定义contextTypes
。chrome
解决办法:npm
- 安装prop-type
- import PropTypes from 'prop-types'
修改以前的代码为:axios
ToDoList.propTypes={
title:PropTypes.string
}
复制代码
网上大部分的解决方法是配置跨域,尝试以后发现仍是不行。而后我修改了axios发出请求的的baseurl,就消除了这个问题。api
这个问题我还没解决,多是跟缓存有关系,或者跟浏览器插件有关,总之出现这个错误提示代表客户端请求都没有发送,服务端天然也接收不到。