async asyncData({store, route}) {
await store.dispatch('GET_PRODUCT', route.params.id);
},
GET_PRODUCT: async ({ commit }, id) => {
try {
const RES = await productApi.GET_PRODUCT(id);
await commit('SET_PRODUCT', RES.data);
} catch (e) {
console.log(e.response);
}
},
{ error: '404 error' }
$router.push({name: '404'})
? $router
<template>
<v-container fluid>
<v-layout
column
fill-height
align-center
>
<v-flex>
<h2 v-if="error.statusCode === '404'">Страница не найдена</h2>
<h2 v-if="error.statusCode === '500'">{{error.message}}</h2>
<h3 v-else>Произошла ошибка</h3>
<nuxt-link to="/">Вернуться на главную</nuxt-link>
</v-flex>
</v-layout>
</v-container>
</template>
<script>
export default {
name: "error",
props: ["error"]
}
</script>
<style scoped>
</style>
Если продукта нету в базе, апи мне возвращает ошибку 404.
{
"code": 0,
"count": 1,
"items": [
{
"name": "Ресторан \"Клот Моне\"",
"home": null,
"phone": null,
"site": null,
"schedule": null,
"latitude": "1.000000",
"longitude": "1.000000",
"id": "3",
"city": null,
"street": null,
"country": null,
"rating": "0.0",
"services": [
{
"name": "Домашняя",
"id": 3,
"group": {
"id": 2,
"name": "Кухня"
}
},
{
"name": "Французкая",
"id": 20,
"group": {
"id": 2,
"name": "Кухня"
}
}
],
"images": [],
"post_code": null
}
]
}
{
path: ':catalogName',
name: 'catalog',
component: () => import('Views/catalog')
}
const router = new VueRouter({
routes: [
{
path: '/:catalogName',
component: Foo,
beforeEnter: (to, from, next) => {
// нужно выполнить проверку params действительно ли есть параметр catalogName в from.params
if (если не в порядке) {
next('/error') // например посылаем на маршрут, возможно с дополнительным параметрами
}
next()
}
}
]
})
Vue.use(Router)
function route (path, view, name, meta) {
return {
name: name || view,
path,
meta,
component: () => import(`~/pages/${view}.vue`).then(m => m.default || m)
}
}
export function createRouter() {
return new Router({
mode: 'history',
routes: paths.map(path => route(path.path, path.view, path.name, path.meta)).concat([{ path: "*", redirect: "/" }])
})
}
export default [
{
path: "/",
name: "index",
view: "index",
meta: {
title: "Справочник компаний",
breadcrumbs: [
{name: "Главная", href: "/"}
]
}
},
{
path: "/organizations",
name: "organizations",
view: "organizations",
props: true,
meta: {
title: "Организации",
breadcrumbs: [
{name: "Главная", href: "/"},
{name: "Организации"},
]
}
},
{
path: "/organization",
name: "organization",
view: "organization",
meta: {
breadcrumbs: [
{name: "Главная", href: "/"},
{name: "Организация"},
]
}
}
]
const router = new VueRouter({
routes: [
{
path: '/:catalogName',
component: Foo,
beforeEnter: (to, from, next) => {
// здесь будут происходить события перед переходом по этому маршруту
// next() разрешает перейти дальше или next(перенаправляю на другой маршрут)
// потому что данные не соответствуют паттерну в from.params или from.query
}
}
]
})