Created
June 19, 2019 18:26
-
-
Save albenik/5072c33b20b96c3ba78c4e16f6132694 to your computer and use it in GitHub Desktop.
vue + axios + reddit little hint
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <template> | |
| <ul v-for="r in reddits"> | |
| <li><h4>{{r.title}}</h4><p>{{r.text}}</p></li> | |
| </ul> | |
| </template> | |
| <script> | |
| import { retrieveReddits } from './data'; // No nedd to specify js extension here. It will be added implicitely | |
| export default { | |
| name: 'RedditsList', | |
| data: () => ({ | |
| reddits: [], | |
| }); | |
| mounted() { | |
| retrieveReddits() | |
| .then(data => { | |
| reddits = data; | |
| }) | |
| .catch(reason => { | |
| console.error(reason); | |
| alert('Unable to retrieve reddits'); | |
| }); | |
| }, | |
| } | |
| </script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import axios from 'axios'; | |
| const reddits = [ | |
| {title: 'Reddit 1', text: 'Reddit 1 text'}, | |
| {title: 'Reddit 2', text: 'Reddit 2 text'}, | |
| {title: 'Reddit 3', text: 'Reddit 3 text'}, // <-- You can keep comma here (at last line) | |
| ] | |
| export function retrieveReddits() { | |
| return new Promise(resolve => { | |
| resolve(reddits); | |
| }); | |
| } | |
| export function retrieveRedditsFromAPI() { | |
| // This is only inaccurate code with possible error and icorrect field named, but it show the way | |
| return axios.get('https://www.reddit.com/r/PublicFreakout/.json?sort=top&t=month') | |
| .then(data => { | |
| return data.children.map(record => { | |
| return {record.title, record.text}; | |
| }); | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment