如何根据return值改变window.location.href?

How to change window.location.href according to return value?

我在渲染函数中使用它

    @observable private redirectUrl: string = null;

    public async componentWillMount() {
        this.redirectUrl = await this.getRedirectUrl();
    }

    public render() {
        if (this.redirectUrl) {
            window.location.href = this.redirectUrl;
            return null;
    } 



其中 redirectUrl 是可观察的并通过异步函数获取。但它在 componentWillMount 函数中不起作用。我认为原因是它是通过异步获取的。

如何解决这个问题?

由于 async 函数将 return 一个承诺,您可以执行以下操作,

async componentWillMount () {
    await anotherAsyncFunction()
    window.location.href = this.redirectUrl
}

希望能奏效。