Ionic 已忽略承诺的窗口打开

yvgpqqbh  于 2022-12-08  发布在  Ionic
关注(0)|答案(1)|浏览(157)

I'm trying to redirect a user to twitter if they choose to. For that I use window.open but it just ignores that instruction. It works fine on other pages but in this function it's just not working. Any suggestions?

answerQuestion() {

        if (!this.isAnswerValid()) {
            this.presentToast('Answer must be between 1 and 140 characters')
            return
        }

        this.loading.present()
        this.questionsService.answerQuestion(this.question, this.answerText, this.imageURL)
        .then(res => {
            if (res) {
                if (this.twitter) {
                    let q = this.question.question
                    if (q.length > 100) {
                        q = q.substring(0, 101)
                    }
    
                    let a = this.answerText
                    if (a.length > 100) {
                        a = a.substring(0, 101)
                    }
                    const url = 'https://nonicapp.web.app/user/' + this.auth.user?.username
                    const tweet = q + " - " + a + " "
                    window.open('https://twitter.com/intent/tweet?text=' + tweet + '&hashtags=nonic&url=' + url, '_system')
                }
                this.router.navigateByUrl('/inbox', { replaceUrl: true });
            } else {
                this.presentToast('Something went wrong, please try again.')
            }
        })
        .catch(err => {
            console.warn(err)
            this.presentToast('Something went wrong, please try again.')
        })
        .finally(() => {
            this.loading.dismiss()
        })
    }
8ulbf1ek

8ulbf1ek1#

我在使用window.open时也遇到了一些问题,所以我开始使用Browser capacitor plugin
很简单

import { Browser } from '@capacitor/browser';

... 

async openUrl(url) {
    await Browser.open({ url: url });
};

相关问题