时出现意外令牌错误

时出现意外令牌错误

本文介绍了使用 react-native-system-setting 时出现意外令牌错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了在电话中启用位置,我使用了 react-native-system-setting .但是在使用它时,我遇到了意外的令牌错误.有人可以澄清我,如何在本机代码中使用它.

For enabling location in Phone i used react-native-system-setting . But while using that i got unexpected token error . can some one clarify me , how to use this in react native codes .

// 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>
    );
  }
}

推荐答案

你应该把它包裹在 constructor()componentWillMount()

you should wrap it in constructor() or componentWillMount()

还有 SystemSetting.switchLocation() 应该只在 SystemSetting.isLocationEnabled() 如果你想启用它返回 false 时调用它

also SystemSetting.switchLocation() should be only call it when the SystemSetting.isLocationEnabled() return false if you want to enable it

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>
 );
}
}

这篇关于使用 react-native-system-setting 时出现意外令牌错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 20:16