Vue js: window.addEventListener ne se trigger pas - PullRequest
1 голос
/ 28 февраля 2020

Моя проблема в том, что я пытаюсь получить токен доступа от Spotify, но мой window.addEventListener ("message, function () {...}") никогда не срабатывает. См. Код ниже:

          mounted(){
    this.$nextTick(function() {
        function login(callback) {
            var CLIENT_ID = 'd3ebae5610894ca48c9f66794214252b';
            var REDIRECT_URI = 'http://localhost:8080/spotify';
            function getLoginURL(scopes) {
                console.log('url')
                return 'https://accounts.spotify.com/authorize?client_id=' + CLIENT_ID +
                '&redirect_uri=' + encodeURIComponent(REDIRECT_URI) +
                '&scope=' + encodeURIComponent(scopes.join(' ')) +
                '&response_type=token';
            }
            var url = getLoginURL([
                'user-read-email'
            ]);
            var width = 450,
            height = 730,
            left = (screen.width / 2) - (width / 2),
            top = (screen.height / 2) - (height / 2);
            var w = window.open(url,
                'Spotify',
                'menubar=no,location=no,resizable=no,scrollbars=no,status=no, width=' + width + ', height=' + height + ', top=' + top + ', left=' + left
            );
            window.addEventListener("message", function(event) {
                var hash = JSON.parse(event.data);
                console.log(JSON.parse(event.data))
                if (hash.type == 'access_token') {
                    callback(hash.access_token);
                }
            }, false);
        }
        var loginButton = document.getElementById('btn-login');
        loginButton.addEventListener('click', function() {
            console.log('click the button')
            login(function(accessToken) {
                console.log(accessToken)
            });
        });
    })}
...