我将在Rails 6中开始使用actionCable,它工作得很好,但是 我不知道为什么在poke_channel.js中未定义“ .perform”方法。 请我帮忙
# in app/channels/poke_channel.rb
class PokeChannel < ApplicationCable::Channel
def subscribed
stream_from "poke"
end
def unsubscribed
# Any cleanup needed when channel is unsubscribed
end
def poke(data)
actionCable.server.broadcast('poke')
end
end
# in app/javascript/channels/poke_channels.js
import consumer from "./consumer"
consumer.subscriptions.create("PokeChannel",{
connected() {
$('#poke-btn').click(function(event) {
this.perform("poke",{});
});
// Called when the subscription is ready for use on the server
},disconnected() {
// Called when the subscription has been terminated by the server
},received(data) {
alert('Poked');
// Called when there's incoming data on the websocket for this channel
},});
# in app/views/messages/index.html.erb
<button id="poke-btn">Poke</button>
当我单击按钮时,
poke_channel.js:6 Uncaught TypeError: this.perform is not a function
at HTMLButtonElement.<anonymous> (poke_channel.js:6)
at HTMLButtonElement.dispatch (event.js:328)
at HTMLButtonElement.elemData.handle (event.js:148)