React native React Native:获得权限后调用函数

React native React Native:获得权限后调用函数,react-native,React Native,我只需要在获得位置权限后调用一个函数。 这是我的代码: componentDidMount() { this.samplePermissionRequest(); alert('componentDidMount'); } samplePermissionRequest=()=>{ async function requestLocationPermission() { try { const granted = await

我只需要在获得位置权限后调用一个函数。 这是我的代码:

  componentDidMount() {
    this.samplePermissionRequest();
    alert('componentDidMount');
  }

  samplePermissionRequest=()=>{
    async function requestLocationPermission() {
      try {
        const granted = await PermissionsAndroid.request(
          PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
          {
            'title': 'Cool Location tracking App Permission',
            'message': 'This Cool App needs access to your location '
          }
        )
        if (granted === PermissionsAndroid.RESULTS.GRANTED) {
          this.justStart();
          alert("You can use the app");
        } else {
          alert("You can not use the app");
        }
      } catch (err) {
        console.warn(err);
      }
    }
    requestLocationPermission();
  }

  justStart=()=>{
      alert("start with permission");
  }
当我运行这段代码时,我只得到第一个警报的对话框(警报('componentDidMount'))

如果删除行“this.justStart();”,则还会显示第二个警报。所以问题是“this.justStart();”不起作用


为什么?

您必须使用Promise对象,您可以这样更改代码:

componentDidMount() {
    this.requestLocationPermission().then(
        alert('componentDidMount')
    );
}


async requestLocationPermission() {
    return PermissionsAndroid.request(
        PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
        {
            'title': 'Cool Location tracking App Permission',
            'message': 'This Cool App needs access to your location '
        }
    ).then(granted => {
        if (granted === PermissionsAndroid.RESULTS.GRANTED) {
            this.justStart();
            alert("You can use the app");
        } else {
            alert("You can not use the app");
        }
    }).catch(e => console.warn(e));
}

在保证异步方法返回时,您可以使用.then and.catch

您必须使用Promise对象,您可以如下更改代码:

componentDidMount() {
    this.requestLocationPermission().then(
        alert('componentDidMount')
    );
}


async requestLocationPermission() {
    return PermissionsAndroid.request(
        PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
        {
            'title': 'Cool Location tracking App Permission',
            'message': 'This Cool App needs access to your location '
        }
    ).then(granted => {
        if (granted === PermissionsAndroid.RESULTS.GRANTED) {
            this.justStart();
            alert("You can use the app");
        } else {
            alert("You can not use the app");
        }
    }).catch(e => console.warn(e));
}

在保证异步方法返回时,您可以使用.then和.catch,因为
这个
将在
requestLocationPermission
函数中
未定义。
因此,
this.justStart()
将抛出类型错误,该错误将被try/catch语句捕获

解决方案1

调用
requestLocationPermission
时绑定

requestLocationPermission.call(此)

解决方案2

删除函数
requestLocationPermission
,直接使用
samplePermissionRequest
,而不是使用内部函数并再次调用它

async samplePermissionRequest = () => {
    try {
      const granted = await PermissionsAndroid.request(
        PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
        {
          'title': 'Cool Location tracking App Permission',
          'message': 'This Cool App needs access to your location '
        }
      )
      if (granted === PermissionsAndroid.RESULTS.GRANTED) {
        this.justStart();
        alert("You can use the app");
      } else {
        alert("You can not use the app");
      }
    } catch (err) {
      console.warn(err);
    }
  }

希望有帮助。

因为
这个
将在
requestLocationPermission
函数中
未定义。
因此,
this.justStart()
将抛出类型错误,该错误将被try/catch语句捕获

解决方案1

调用
requestLocationPermission
时绑定

requestLocationPermission.call(此)

解决方案2

删除函数
requestLocationPermission
,直接使用
samplePermissionRequest
,而不是使用内部函数并再次调用它

async samplePermissionRequest = () => {
    try {
      const granted = await PermissionsAndroid.request(
        PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
        {
          'title': 'Cool Location tracking App Permission',
          'message': 'This Cool App needs access to your location '
        }
      )
      if (granted === PermissionsAndroid.RESULTS.GRANTED) {
        this.justStart();
        alert("You can use the app");
      } else {
        alert("You can not use the app");
      }
    } catch (err) {
      console.warn(err);
    }
  }
希望能有帮助