You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/components/HackerNews.vue
+74-5Lines changed: 74 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,8 @@
4
4
<p>Before create it, we need to prepare the following things:</p>
5
5
<ol>
6
6
<li>Hacker News API, we used <atarget="_blank"href="https://hn.algolia.com/api">HN Search API</a> in this case;</li>
7
-
<li>Import <atarget="_blank"href="https://github.com/vuejs/vue-resource">vue-resource</a> plugin to request news data.</li>
7
+
<liv-show="$parent.docVersion < 2">Import the <atarget="_blank"href="https://github.com/vuejs/vue-resource">vue-resource</a> plugin to request news data.</li>
8
+
<liv-show="$parent.docVersion >= 2">Import the <atarget="_blank"href="https://github.com/mzabriskie/axios">axios</a> library to request news data.</li>
<p>In the template, we create a header and a list for Hacker News. For <code>InfiniteLoading</code> component in this case, unlike the basic use, we customized the content of the no more data prompt base on <code>slot</code>.</p>
71
102
<h4>Script</h4>
@@ -131,7 +162,8 @@ export default {
131
162
InfiniteLoading,
132
163
},
133
164
};</pre>
134
-
<prev-highlightjsv-if="$parent.docVersion >= 2">import InfiniteLoading from 'vue-infinite-loading';
165
+
<prev-highlightjsv-if="$parent.docVersion >= 2 && $parent.docVersion < 2.2">import InfiniteLoading from 'vue-infinite-loading';
166
+
import axios from 'axios';
135
167
136
168
const api = 'http://hn.algolia.com/api/v1/search_by_date?tags=story';
137
169
@@ -143,7 +175,7 @@ export default {
143
175
},
144
176
methods: {
145
177
onInfinite() {
146
-
this.$http.get(api, {
178
+
axios.get(api, {
147
179
params: {
148
180
page: this.list.length / 20 + 1,
149
181
},
@@ -163,8 +195,45 @@ export default {
163
195
components: {
164
196
InfiniteLoading,
165
197
},
198
+
};</pre>
199
+
<prev-highlightjsv-if="$parent.docVersion >= 2.2">import InfiniteLoading from 'vue-infinite-loading';
200
+
import axios from 'axios';
201
+
202
+
const api = 'http://hn.algolia.com/api/v1/search_by_date?tags=story';
203
+
204
+
export default {
205
+
data() {
206
+
return {
207
+
list: [],
208
+
};
209
+
},
210
+
methods: {
211
+
infiniteHandler($state) {
212
+
axios.get(api, {
213
+
params: {
214
+
page: this.list.length / 20 + 1,
215
+
},
216
+
}).then(res => res.json()).then((data) => {
217
+
if (data.hits.length) {
218
+
this.list = this.list.concat(data.hits);
219
+
$state.loaded();
220
+
if (this.list.length / 20 === 10) {
221
+
$state.complete();
222
+
}
223
+
} else {
224
+
$state.complete();
225
+
}
226
+
});
227
+
},
228
+
},
229
+
components: {
230
+
InfiniteLoading,
231
+
},
166
232
};</pre>
167
233
<p>
168
-
In the <code>onInfinite</code> function, we request a page of news and pushed them into list everytime, if we had been requested 10 pages of news, send an <codev-show="$parent.docVersion < 1">$InfiniteLoading:noMore</code><codev-show="$parent.docVersion >= 1">$InfiniteLoading:complete</code> event to tell the <code>InfiniteLoading</code> component that there is no more data now, it will display the no more data prompt that we customized in template for user.
234
+
In the <code>onInfinite</code> function, we request a page of news and pushed them into list everytime, if we had been requested 10 pages of news,
235
+
<spanv-show="$parent.docVersion < 2.2">send an <codev-show="$parent.docVersion < 1">$InfiniteLoading:noMore</code><codev-show="$parent.docVersion >= 1">$InfiniteLoading:complete</code> event</span>
236
+
<spanv-show="$parent.docVersion >= 2.2">call the <code>complete</code> method of the <code>$state</code> special event argument</span>
237
+
to tell the <code>InfiniteLoading</code> component that there is no more data now, it will display the no more data prompt that we customized in template for user.
0 commit comments