0

一段时间以来,我一直在学习 Vue Native。我正在浏览https://vue-native.io中提供的文档并一一尝试。当我来到 Geolocation 部分时,我想试用 expo permision api。我收到了这个错误

undefined 不是对象(评估 '_expo.Permissions.askAsync')

这是我的代码

 <template>
  <view class="container">
    <text>Location:</text>
    <text>{{location.latitude}}</text>
    <touchable-opacity :on-press="getLocation" >
        <text>get location</text>
    </touchable-opacity>
  </view>
</template>

 <script>

import { Constants, Location, Permissions } from "expo";

export default {
  data: function() {
    return {
      location: {},
      errorMessage: ""
    };
  },
  methods: {
    getLocation: function() {
      Permissions.askAsync(Permissions.LOCATION).then(status => {
        if (status !== "granted") {
          errorMessage = "Permission to access location was denied";
        }
        Location.getCurrentPositionAsync({}).then(location1 => {
          location = location1;
        });
      }).catch((err)=>{
        console.log(err);
     });
    }
  }
};
</script>
<style>
.container {
  background-color: white;
  align-items: center;
  justify-content: center;
  flex: 1;
}
.text-color-primary {
  color: blue;
}
</style>

该错误必须来自 getLocation 函数并且与 expo 权限有关。你能告诉我这里可能是什么问题吗?谢谢

4

1 回答 1

-1

尝试这个:

const _this = this;
Permissions.askAsync(Permissions.LOCATION)
    .then( status => {
        if (status !== "granted") {
            errorMessage = "Permission to access location was denied";
        } else {
            _this.Location.getCurrentPositionAsync()
               .then(location1 => {
                  _this.location = location1;
               })
        }
    })
    .catch( error => { console.log('error');})
于 2021-02-12T21:19:50.267 回答