vue2 + vuex + sockJS
1.安装依赖sockjs + sockjs-client
2.创建store.js 和在src=>utils文件夹下新建socket.js
3.在index.html中引入sockjs.min.js和stomp.min.js
4.socket.js文件内容
import store from '../store'
export function connectSocket() {
let wsurl = url (url是你后台向前端主动推送数据的地址)
let socket = new SockJS(wsurl );
let stompClient = Stomp.over(socket );
stompClient.connect({},()=>{
stompClient.subscribe('/topic/xxxx',(greeting)=>{
let greetingData = JSON.parse(greeting.body) //处理返回的数据格式
store.commit('modiyData',greetingData) // 存到vuex里面
})
})
};
5.vueX内容
state:{
mapData:{}.
}
mutations:{
modiyData(state,mapData) {
state.mapData = mapData
}
}
getters: {
getMapData(state) {
return state.mapData
}
}
4.main.js文件中 引入并且调用
import { connectionSocket } from ‘./utils/socket’
connectionSocket ()
5.