【发布时间】:2019-05-11 05:50:16
【问题描述】:
我正在尝试使用 VueJS 中的 Vuex 存储连接 signalR,但是当连接订阅时,我收到错误
我将存储库分成几个文件
存储 index.js
import Vue from 'vue'
import Vuex from 'vuex'
import SignalR from './modules/signalR'
Vue.use(Vuex)
const store = () =>
new Vuex.Store({
modules: {
signalR: SignalR
}
});
export default store
SignalR主连接文件signalR/index.js
const signalR = require('@aspnet/signalr')
const Cookie = process.client ? require("js-cookie") : undefined;
export default {
state: {
connection: null
},
我在应用程序的主要组件中声明我通过调度引起的操作
actions: {
connectSignalR(context, data) {
var token = Cookie.get("authToken");
console.log('signalR', token)
context.state.connection == null ? context.state = new signalR.HubConnectionBuilder().withUrl(context.getters.base_signalr + "/notification",
{ accessTokenFactory: () => token }).build()
: false
let startedPromise = null
function start() {
startedPromise = context.state.connection.start()
.catch(error => {
console.log('Failed to connect with hun', error)
return new Promise((resolve, reject) =>
setTimeout(() => start().then(resolve).catch(reject), 5000))
})
return startedPromise
}
context.state.connection.onclose(() => start())
//context.state.connection.connectionSlow(function() {
// console.log('slow connection...');
//});
context.state.connection.on('support', (data) => {
console.log(data);
})
start()
}
}
}
错误
TypeError: context.state.connection.connectionSlow is not a function
at Store.connectSignalR
或
TypeError: context.state.connection.on is not a function
at Store.connectSignalR
【问题讨论】:
标签: javascript node.js vue.js signalr