Explorar o código

更新 Index.vue 的 SSE 推送

reghao %!s(int64=2) %!d(string=hai) anos
pai
achega
61f6ec244e
Modificáronse 1 ficheiros con 6 adicións e 4 borrados
  1. 6 4
      src/views/home/Index.vue

+ 6 - 4
src/views/home/Index.vue

@@ -142,17 +142,19 @@ export default {
       })
     },
     initServerSendEvent() {
-      var list = this.carouselList
       if (typeof (EventSource) !== 'undefined') {
         const sseUrl = 'https://api.reghao.cn/api/data/video/hot'
         const source = new EventSource(sseUrl)
         source.addEventListener('test', function(e) {
           console.log(e)
         })
+
+        const that = this
         source.onmessage = function(event) {
-          console.log(event)
-          console.log(list)
-          // list.push(event.data)
+          const dataList = JSON.parse(event.data)
+          if (dataList.length !== 0) {
+            that.carouselList = dataList
+          }
         }
       } else {
         console.log('抱歉,你的浏览器不支持 SSE...')