Bayanai fetching kuma prefetching dabaru ne masu mahimmanci don ƙirƙirar aikace-aikacen gidan yanar gizo masu ƙarfi da amsawa. A Nuxt.js, waɗannan fasahohin ana amfani da su don inganta lodin bayanai, ko akan uwar garken ko abokin ciniki. A cikin wannan labarin, za mu tattauna yadda ake amfani da kuma kwatanta bayanai fetching da prefetching hanyoyi a cikin Nuxt.js, tare da samar da takamaiman misalai na lamba.
Jagora ga Data Fetching da Prefetching in Nuxt.js
Server-Side Bayanai Fetching:
A cikin Nuxt.js, zaku iya amfani da asyncData
hanyar don ɗauko bayanai kafin yin shafi akan sabar. Misali, bari mu ga yadda zamu iya debo bayanai daga API don nuna jerin sakonni:
export default {
async asyncData() {
const response = await fetch('https://api.example.com/posts');
const data = await response.json();
return { posts: data };
}
}
Lokacin shiga shafin, za a debo bayanan aika kuma a shirye don server-side nunawa.
Bayanan Abokin ciniki Fetching:
Don bayanan gefen abokin ciniki fetching, yi amfani da fetch
hanyar a cikin sassa ko shafuka. Misali, don nuna bayanan post lokacin da mai amfani ya danna hanyar haɗi:
export default {
async fetch() {
const postId = this.$route.params.id;
const response = await fetch(`https://api.example.com/posts/${postId}`);
this.post = await response.json();
}
}
Bayanan Duniya Prefetching:
Don bayanan duniya prefetching, saita nuxt.config.js
fayil ɗin. Misali, don fitar da bayanan mai amfani ga duk shafuka:
export default {
prefetch: [
{ url: '/user', as: 'user', data: { id: 1 } }
]
}
Bayanan Matsayi-Bayani Prefetching:
Yi amfani da prefetch
kayan a matakin bangaren. Misali, don ƙaddamar da bayanai don abin jeri:
export default {
data() {
return {
items: []
};
},
prefetch() {
return this.items.map(item =>({
url: `/details/${item.id}`,
as: `details-${item.id}`,
data: item
}));
}
}
Kammalawa
Bayanai fetching kuma prefetching a cikin Nuxt.js dabaru ne masu mahimmanci don haɓaka lodin bayanai da haɓaka amsa aikace-aikacen. Ta hanyar yin amfani da waɗannan hanyoyin yadda ya kamata da fahimtar bambance-bambancen da ke tsakanin server-side abokan ciniki da hanyoyin haɗin kai, za ku iya ƙirƙirar ƙwarewar mai amfani mai sauƙi da ingantaccen aiki a cikin ayyukanku Nuxt.js.