Skip to content

Instantly share code, notes, and snippets.

View firminochangani's full-sized avatar

Firmino firminochangani

View GitHub Profile
created() {
// Making the http request with Fetch API
const API = 'https://restcountries.eu/rest/v2/all';
fetch(API, {method: 'GET'})
.then(res => res.json())
.then((json) => {
// Place results into the state property
this.countries = json;
})
<body>
<div id="app">
<h1>Vue Search</h1>
<br>
<!-- Search field -->
<input
type="text"
placeholder="Search for a Country"
>
<body>
<!-- Importing -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.0.3/vue.js"></script>
</body>
{
"countries": [
{
"country": "Afghanistan",
"states": ["Badakhshan", "Badghis", "Baghlan", "Balkh", "Bamian", "Daykondi", "Farah", "Faryab", "Ghazni", "Ghowr", "Helmand", "Herat", "Jowzjan", "Kabul", "Kandahar", "Kapisa", "Khost", "Konar", "Kondoz", "Laghman", "Lowgar", "Nangarhar", "Nimruz", "Nurestan", "Oruzgan", "Paktia", "Paktika", "Panjshir", "Parvan", "Samangan", "Sar-e Pol", "Takhar", "Vardak", "Zabol"]
},
{
"country": "Albania",
"states": ["Berat", "Dibres", "Durres", "Elbasan", "Fier", "Gjirokastre", "Korce", "Kukes", "Lezhe", "Shkoder", "Tirane", "Vlore"]
},
created() {
const API_POSTS = 'https://jsonplaceholder.typicode.com/posts';
fetch(API_POSTS, {method: 'GET'})
.then(res => res.json())
.then(json => {
this.posts = json;
/* Set loading to false after the content
is ready to be displayed */
this.loading = false;
})
const vueapp = new Vue({
el: '#vueapp',
data() {
return {
msg: 'Hello World',
posts: [],
loading: true
};
},
// Other part of the code
<section id="vueapp">
<article
class="post"
v-for="post in posts"
:key="post.id"
v-if="!loading"
>
<h1 class="title">{{ post.title }}</h1>
<p>{{ post.body }}</p>
</article>
<section id="vueapp">
<article class="post" v-for="post in posts" :key="post.id">
<h1 class="title">{{ post.title }}</h1>
<p>{{ post.body }}</p>
</article>
</section>
<script>
const vueapp = new Vue({
el: '#vueapp',
data() {
return {
posts: []
};
},
created() {
const API_POSTS = 'https://jsonplaceholder.typicode.com/posts';
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Vue.js</title>
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
</head>
<body>