js如何处理这样的异步问题?

作者站长头像
站长
· 阅读数 8

定义一个class类js如何处理这样的异步问题?

使用js如何处理这样的异步问题?

在使用的时候发现在created无法获取authmap的值,大家有什么好思路吗?

回复
1个回答
avatar
test
2024-07-12
        class fun {
            constructor() {
                this.authMap = {}
                // this.initAuth()
            }
            async initAuth() {
                const isshowCreateApp = await getAuthorization()
                this.setAuthMap({ createAuth: isshowCreateApp })
            }
            setAuthMap(authmap) {
                this.authMap = {
                    ...authmap
                }
                console.log(this.authMap, 'this.authMap')
            }
        }
        function getAuthorization() {
           return new Promise((res) => {
                setTimeout(() => {
                    res('666')
                },)
            })
        }
        async function fn() {
            let connector = new fun()
            await connector.initAuth()
            console.log(connector, "this.connector.authMap")
        }
        fn()
这样的话不用改你自己的代码
<script>
import fun from './1.js'
export default {
  data () {
    return {
      connector: new fun()
    }
  },
  watch: {
    'connector.authMap' (newVal, oldVal) {
      console.log(this.connector.authMap);
    }
  }
}
</script>
回复
likes
适合作为回答的
  • 经过验证的有效解决办法
  • 自己的经验指引,对解决问题有帮助
  • 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
  • 询问内容细节或回复楼层
  • 与题目无关的内容
  • “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容