Я хочу отобразить фид оповещений Google на своей «Личной панели». vue »из компонента« Фид. 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);
});
});
}
}
}