在使用ActionCable時,
app/assets/javascripts/channels/calladdresses.coffee: App.calladdress = App.cable.subscriptions.create "CalladdressChannel", connected: -> # Called when the subscription is ready for use on the server disconnected: -> # Called when the subscription has been terminated by the server received: (data) -> console.log(data) document.getElementById("app").innerHTML = data.title;
提示在chrome控制台 innerHTML is null 報告❌。
這是因為此時DOM還沒有加載完成。
解決方法:
使用:
document.addEventListener "turbolinks:load", -> document.getElementById("app").innerHTML = data.title;
傳統做法是window.onload = function {} .