Browse Source

improvement: 将ws创建过程放到mounted(created可能有潜在bug)

ChenYangfan 6 years ago
parent
commit
1c981456b2
2 changed files with 13 additions and 12 deletions
  1. 0 0
      src/common-style.scss
  2. 13 12
      src/components/DiscussionArea.vue

+ 0 - 0
src/common-style.scss


+ 13 - 12
src/components/DiscussionArea.vue

@@ -44,6 +44,7 @@ export default {
     }
   },
   mounted () {
+    this.initWs()
     if (!this.userInfo) {
       this.$setInfoMessage('需要登录本系统才可以浏览课程内容')
       this.$router.push('login')
@@ -66,22 +67,22 @@ export default {
       this.updateComments()
     }
   },
-  created () {
-    this._ws = new WebsocketPublisher(useCommentWsServer('/slide/') + this.slide.id)
-    this._ws.subscribe({
-      onNext: data => {
-        this.wsComments.unshift(data)
-      },
-      onError: err => {
-        this.$setErrorMessage(err)
-      },
-      onClose: () => {}
-    })
-  },
   beforeDestroy () {
     this._ws.close()
   },
   methods: {
+    initWs () {
+      this._ws = new WebsocketPublisher(useCommentWsServer('/slide/') + this.slide.id)
+      this._ws.subscribe({
+        onNext: data => {
+          this.wsComments.unshift(data)
+        },
+        onError: err => {
+          this.$setErrorMessage(err)
+        },
+        onClose: () => {}
+      })
+    },
     updateComments () {
       this.getComments(30).then(() => {
         this.getComments(10000)