VueJS和axios:不能在axios响应中使用url在新窗口中打开

问题描述 投票:0回答:1

我在vuejs组件中有以下两种方法:

    openLender: function(setUrl) {
        const win = window.open(setUrl, '_blank');
        win.focus();
    },
    getLender: function(setEnt, setEx, setRev, setCred, setDur, checkWishes, lender) {
        const vm = this;
        const request = 'lender_click';
        const setWishes = vm.arrayToString(checkWishes);
        axios({
            method:'get',
            url:'/api',
            params: {
                request: request,
                setent: setEnt,
                setex: setEx,
                setrev: setRev,
                setcred: setCred,
                setdur: setDur,
                setwishes: setWishes,
                setlender: lender
            },
            responseType:'json'
        })
        .then(function(response) {
            const url = response.data;
            vm.openLender(url[0].response);
        })
        .catch(function(error) {
            alert(error);
        });
    }

问题是我得到这个错误:

TypeError: Cannot read property 'focus' of null

当我console.log(url[0].response)响应,它确实显示我从axios请求得到的网址,但当我使用openLender()方法时,它给了我这个错误。

有什么想法吗?

编辑

我在this帖子中使用了@IgorDymov他的解决方案来解决这个问题

vue.js axios
1个回答
0
投票

window.open方法总是被浏览器阻止,如果它不是由click事件直接引起的。关闭弹出窗口,它会起作用。

您可以在同一窗口中打开该URL,或者为用户提供指向新页面的链接。

© www.soinside.com 2019 - 2024. All rights reserved.