<component :is="stream-display" :stream="stream" v-for="(stream, key) in streams" :key="key"></component>
<template id="stream-component">
<div class="card">
<div class="card-header">{{ stream.name }}</div>
<div class="card-body"><pre>{{ stream }}</pre></div>
</div>
</template>
<script>
Vue.component('stream-display', {
props: ['stream'],
template: '#stream-component'
});
const vm = new Vue({
el: '#app',
data: {
streams: null
},
created: function() {
const self = this;
const eventSource = new EventSource('my-url');
eventSource.addEventListener('message', function (evt) {
const data = JSON.parse(evt.data);
// each message has a unique id for the stream it is for
self.streams = self.streams || {};
self.streams[data.id] = data;
});
}
});
以上与我的设置非常相似。不过,我对此有两个问题。要么显示未更新,要么当一个流结束时,其他流停止更新。