在我的反应应用程序中,我尝试使用 Cordova 插件(cordovo-plugin-device)来获取用户的设备版本,如下所示
class LogoHeader extends React.Component {
componentDidMount () {
window.addEventListener('deviceready', this.onDeviceReady)
}
onDeviceReady () {
console.log(device.cordova)
}
render () {
...
}
}
Run Code Online (Sandbox Code Playgroud)
但出于某种原因,该deviceready事件永远不会被触发。我错过了什么吗?有没有更好的方法在 react 中监听 DOM 事件?
根据JSX 参考,这就是 try-catch 语句的样子
try {
statement*
} [catch (varname : type) {
statement*
}]* [finally {
statement*
}]
Run Code Online (Sandbox Code Playgroud)
我尝试了以下
try {
console.log(window.device.version)
} catch (e : TypeError) {
console.log('Error')
}
Run Code Online (Sandbox Code Playgroud)
这导致错误
模块构建失败:语法错误:意外令牌,预期)(11:15)
Run Code Online (Sandbox Code Playgroud)9 | try { 10 | console.log(window.device.version) 11 | } catch (e : TypeError) { | ^ 12 | console.log('Error') 13 | } 14 | return (
那么在 JSX 中使用 try-catch 语句的正确方法是什么?