开始使用一个小型的vuejs
应用程序.如何在根组件中打开websocket连接,并在其他组件中重用相同的连接?
我希望组件能够通过相同的连接发送和接收.
应用程序.vue:
<template>
<div id="app">
<h1>My app</h1>
<router-link to="/">P&L</router-link>
<router-link to="/other-page">other page</router-link>
<router-view></router-view>
</div>
</template>
<script>
export default {
name: 'app',
data () {
return {
ws: null
}
},
created () {
this.ws = new WebSocket('ws://localhost:8123/ws')
},
methods: {
}
}
}
</script>
现在我想重复使用other-page
中的ws
,这样就不会在每次改变路由时重新连接.