首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在Vue Native中执行onAuthStateChanged?

如何在Vue Native中执行onAuthStateChanged?
EN

Stack Overflow用户
提问于 2020-12-14 16:34:54
回答 1查看 56关注 0票数 0

我想执行一些等同于以下内容的操作:

代码语言:javascript
运行
复制
export default class LoadingScreen extends React.Component {
  componentDidMount() {
    firebase.auth().onAuthStateChanged(user => {
      this.props.navigation.navigate(user ? 'App' : 'Auth');
    });
  }

  render() {
    return (
      <View style={styles.container}>
        <Text>Loading</Text>
        <ActivityIndicator size='large'></ActivityIndicator>
      </View>
    );
  }
}

我已经设置了我的路由,以便在进入AuthApp之前通过加载来检查用户的身份验证状态。当我添加mounted()时,加载文本和活动指示器没有显示。

代码语言:javascript
运行
复制
 <template>
      <view class="container">
        <nb-text>Loading</nb-text>
        <activity-indicator size="large" color="#0000ff" />
      </view>
    </template>
    
    <script>
    import firebase from "firebase";
    import Fire from "./../../api/firebaseAPI";
    
    export default {
      // Declare `navigation` as a prop
      props: {
        navigation: {
          type: Object,
        },
      },
      async mounted() {
        await firebase.auth().onAuthStateChanged(function (user) {
            this.navigation.navigate(user ? "App" : "Auth");
        });
      },
    };
    </script>

当我运行上面的代码时,我得到一个白屏。

EN

回答 1

Stack Overflow用户

发布于 2020-12-29 04:09:40

也许我们应该将代码移入挂载到某个方法中。

代码语言:javascript
运行
复制
<script>
mounted(){
this.checkAuth()
},
...
methods:{
async checkAuth(){
await firebase.auth().onAuthStateChanged(function (user) {
          if(user){
this.navigation.navigate("Auth")
}else{

this.navigation.navigate("App")
}
        });
}

}
</script>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65285852

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档