Middleware muhimmin ra'ayi ne a cikin ci gaban yanar gizo wanda ke taimakawa sarrafawa da sarrafa buƙatun kafin su kai ga ainihin route masu sarrafa. A cikin Nuxt.js, middleware yana taka muhimmiyar rawa wajen sarrafa tantancewa, izini, da aiwatar da ayyuka kafin yin shafi. Wannan labarin zai ba da bayani game middleware da aikace-aikacen sa a cikin Nuxt.js, sannan kuma jagora kan tantance mai amfani da aiwatar da ayyuka kafin loda shafi.
Fahimtar Middleware da Amfaninsa a ciki Nuxt.js
Middleware yana aiki azaman gada tsakanin uwar garken da route masu aiki, yana ba ku damar aiwatar da lamba kafin isa wurin route. A cikin Nuxt.js, middleware ana iya amfani da shi a duniya ko a kan kowane hanya. Wannan yana ba ku damar ayyana ayyukan gama gari, kamar bincikar tantancewa, kafin yin kowane shafi.
Tabbatar da mai amfani da Middleware ciki Nuxt.js
Ƙirƙirar Tabbaci Middleware:
Don aiwatar da amincin mai amfani, ƙirƙiri middleware fayil, misali auth.js
,:
export default function({ store, redirect }) {
if(!store.state.authenticated) {
redirect('/login');
}
}
Ana nema Middleware zuwa Routes:
Aiwatar da tabbacin middleware zuwa takamaiman routes a cikin nuxt.config.js
fayil:
export default {
router: {
middleware: 'auth',
routes: [
{ path: '/dashboard', component: 'pages/dashboard.vue' }
]
}
}
Aiwatar da Aiyuka Kafin Loading Page
Middleware don Preloading Data:
Ƙirƙiri middleware don loda bayanai kafin yin shafi:
export default async function({ store }) {
await store.dispatch('fetchData');
}
Ana nema Middleware zuwa Routes:
Aiwatar da preloading data middleware a routes cikin nuxt.config.js
fayil:
export default {
router: {
middleware: 'preloadData',
routes: [
{ path: '/posts', component: 'pages/posts.vue' }
]
}
}
Kammalawa
Middleware yana Nuxt.js ba da tsari mai ƙarfi don sarrafa buƙatun buƙatun, aiwatar da ingantaccen aiki, da aiwatar da ayyuka kafin yin shafuka. Ta hanyar yin amfani da middleware, za ku iya ƙirƙiri amintaccen kuma ingantaccen aikace-aikacen gidan yanar gizo wanda ke sarrafa amincin mai amfani kuma yana yin ayyuka masu mahimmanci don haɓaka ƙwarewar mai amfani da ayyukan aikace-aikacen.