【问题标题】:Displaying RSS feed on Vue.js frontend在 Vue.js 前端显示 RSS 提要
【发布时间】:2020-06-06 21:30:08
【问题描述】:

我想在我的“Dashboard.vue”上显示来自组件“Feed.vue”的谷歌警报提要。我得到了这个在 javascript 中工作,但不知道如何将我的 js 前端转换为 Vue。

我收到错误:io 未定义。关于如何解决这个问题并在我的 Vue.js 应用程序上显示来自提要的文章的任何想法?

后端代码:

const feedparser = require('feedparser-promised');
const express = require('express');
const app = express();
const server = require('http').Server(app);
const io = require('socket.io')(server);
const fs = require('fs');

server.listen(8000);
console.log('Server started on localhost:8000');

let url = 'https://www.google.ie/alerts/feeds/10663948362751557705/4511034072220974544';

// Declare a variable for the feed content
let feed = [];

// Parse the feed
feedparser.parse(url).then(items => {
  // Update the variable with the new data
  for (const [i, item] of items.entries()) {
    // Retrieve the ten first elements, with their title and description
    // And add them to the feed array
    if (i < 9) {
      feed.push({
        title: item.title,
        link: item.link
      });
    }
  }

  // Write it to a file so you can know how to parse it better
  // The callback is mandatory, but it's useless here
  fs.writeFile('feed.json', JSON.stringify(items, null, 2), 'utf-8', (data) => {});
});

// Define the default route
app.get('/', (req, res) => {
  // Render the page
  res.render('App.vue');

  // Send the data to the client using socket.io
  io.on('connection', io => {
    io.emit('feed', {
      feed: feed
    });
  });
});

仪表板.vue

 <template>
   <li>
      {{feed.title}}
   </li>
</template>

<script>
   export default {
      props: ["feed"]
   }
</script>

Feed.vue

  <template>
   <ul>
      <feed v-for="feed in feeds" :feed="feed" :key="feed.title"></feed>
   </ul>
</template>

<script>
   import Feed from './Feed.vue'
   export default {
      components: {
         Feed
      },
      data () {
         return {
            feeds: []
         }
      },
      mounted() {
         this.subscribeToFeed();
      },
      methods: {
         subscribeToFeed() {
            const socket = io();
            socket.on('feed', data => {
               data.feed.entries().forEach(feed => {
                  this.feeds.push(feed);
               });
            });
         }
      }
   }

【问题讨论】:

    标签: javascript node.js api vue.js rss


    【解决方案1】:

    检查这一行:

    const socket = io();
    

    您似乎忘记在这个 vue-component 中导入 io 模块了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-26
      • 1970-01-01
      • 2015-12-01
      相关资源
      最近更新 更多