要在手机中启用位置,我使用了react-native-system-setting
。但在使用时,我得到了意外的令牌错误。有人可以澄清我,如何使用它来反应原生代码。
// this is my error
Unexpected token (12:15)
10 | };
11 |
> 12 | SystemSetting.isLocationEnabled().then((enable)=>{
| ^
13 | const state = enable ? 'On' : 'Off';
14 | console.log('Current location is ' + state);
15 | })

// this is my CODE
import React, { Component } from "react";
import SystemSetting from 'react-native-system-setting';
export default class PhoneSetting extends Component {
SystemSetting.isLocationEnabled().then((enable)=>{
// got error on above line
const state = enable ? 'On' : 'Off';
console.log('Current location is ' + state);
})
SystemSetting.switchLocation(()=>{
console.log('switch location successfully');
})
render() {
var self = this;
return (
<View>
// some content
</View>
);
}
}
&#13;
答案 0 :(得分:0)
您应该将其打包在constructor()
或componentWillMount()
SystemSetting.switchLocation()
也应仅在SystemSetting.isLocationEnabled()
返回false时调用它,如果你想启用它
import React, { Component } from "react";
import SystemSetting from 'react-native-system-setting';
export default class PhoneSetting extends Component {
constructor() {
super();
SystemSetting.isLocationEnabled().then((enable)=>{
// got error on above line
const state = enable ? 'On' : 'Off';
console.log('Current location is ' + state);
})
SystemSetting.switchLocation(()=>{
console.log('switch location successfully');
})
}
render() {
var self = this;
return (
<View>
// some content
</View>
);
}
}