2016-10-19 81 views
1

我想知道你什麼時候從JS做new Pusher("{{env("PUSHER_KEY")}}")它是否創建一個新對象或掛鉤到由PUSHER_KEY標識的已經可用的對象。我現在面臨的問題是,我無法從B.js.訪問定義爲創建Pusher窗體的新實例javascript

var pusher = new Pusher("{{env("PUSHER_KEY")}}"); 
var commonChannel = pusher.subscribe('test-channel'); 
    commonChannel.bind('test-event', function() { 
    location.reload(); 
}); 

推在A.js 當我嘗試從B.js

var commonChannel = pusher.subscribe('test-channel'); 
    commonChannel.bind('test-event', function() { 
    location.reload(); 
}); 

做這樣的事情,我得到ReferenceError: pusher is not defined但是當我重新推動器B.js作爲

var pusherLocal = new Pusher("{{env("PUSHER_KEY")}}") 
var channel = pusherLocal.subscribe('test-channel'); 
channel.bind('test-event', function(data) { 
    alert(data.text); 
}); 

我得到SyntaxError: missing) after argument list錯誤。任何人都可以給我一些指針,我在這裏失蹤?

回答

0

在您的網頁添加該腳本

<script src="https://js.pusher.com/3.2/pusher.min.js"></script> 
<script> 
    // Enable pusher logging - don't include this in production 
    Pusher.logToConsole = true; 

    var pusher = new Pusher('key', { 
     encrypted: true 
    }); 

    var channel = pusher.subscribe('refresh-channel'); 
    channel.bind('refresh-event', function(data) { 
     alert(); 
    }); 
</script>