如何从Vuejs读取stream + json响应?

mgr*_*min 5 rest json vue.js axios

我在vuejs应用程序和以下代码中使用axios http客户端:

axios.get('http://localhost:8081/pets')
    .then(response => {
      this.pets = response.data;
    })
Run Code Online (Sandbox Code Playgroud)

如果服务器返回简单的“ application / json”内容,则一切正常。但我想分别为每一行阅读“ application / stream + json”。

例如:

axios.get('http://localhost:8081/pets')
    .then(response => {
      this.pets.push(response.data)
    })
Run Code Online (Sandbox Code Playgroud)

但是此代码(按预期方式)不起作用。

mgr*_*min 5

我用SSE解决了这个问题:

let es = new EventSource('http://localhost:8081/pets');
es.addEventListener('message', event => {
    let data = JSON.parse(event.data);
    this.pets.push(data);
}, false);
Run Code Online (Sandbox Code Playgroud)