如下设置后,能够连接上本地服务器
import store from './store'
import VueSocketio from 'vue-socket.io';
import socketio from 'socket.io-client';
Vue.use(VueSocketio, socketio('http://127.0.0.1:7777/'),store);
下一步就是获取后台数据后赋值
sockets: {
getData: (data) => {
console.log(this.$store);
}
}
打印出undefined 。 其他地方正常使用store
试过在mounted中打印this是 VueComponent {_uid: 4, _isVue: true, $options: {…}, _renderProxy: Proxy, _self: VueComponent, …}
在sockets中打印this是{a: {…}}
a: {name: "Home", components: {…}, sockets: {…}, mounted: ƒ, methods: {…} , 是当前页面的信息。
该怎么办呢
this.$store
是Vuex
为方便调用而自动注入到实例中每个子组件的store
对象,其实它就是你store.js
中暴露出来的那个store
。在非vue实例组件中调用直接引入即可