【问题标题】:VueJS and axios: can't use url in axios response to open in a new windowVueJS 和 axios:不能在 axios 响应中使用 url 在新窗口中打开
【发布时间】:2018-08-24 08:13:12
【问题描述】:

我在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 请求中获得的 url,但是当我使用 openLender() 方法时,它给了我这个错误。

有什么想法吗?

编辑

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

【问题讨论】:

    标签: vue.js axios


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-02-10
      • 2018-02-16
      • 2012-12-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多