Files
interview-demo-code/frontend/spa/src/router.js
Nikita Kiselev 01458e3b4c
Some checks failed
Telegram Mini App Shop Builder / Compute version metadata (push) Has been cancelled
Telegram Mini App Shop Builder / Run Frontend tests (push) Has been cancelled
Telegram Mini App Shop Builder / Run Backend tests (push) Has been cancelled
Telegram Mini App Shop Builder / Run PHP_CodeSniffer (push) Has been cancelled
Telegram Mini App Shop Builder / Build module. (push) Has been cancelled
Telegram Mini App Shop Builder / release (push) Has been cancelled
Squashed commit message
2026-03-11 22:33:34 +03:00

70 lines
2.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import {createRouter, createWebHashHistory} from 'vue-router';
import Home from './views/Home.vue';
import Product from './views/Product.vue';
import CategoriesList from "./views/CategoriesList.vue";
import Cart from "./views/Cart.vue";
import Products from "@/views/Products.vue";
import Checkout from "@/views/Checkout.vue";
import OrderCreated from "@/views/OrderCreated.vue";
import Search from "@/views/Search.vue";
import Filters from "@/views/Filters.vue";
import Account from "@/views/Account.vue";
import {useYaMetrikaStore} from "@/stores/yaMetrikaStore.js";
const routes = [
{
path: '/',
name: 'home',
component: Home,
},
{path: '/filters', name: 'filters', component: Filters},
{path: '/product/:id', name: 'product.show', component: Product},
{
path: '/products/:category_id',
name: 'product.categories.show',
component: Products,
},
{path: '/categories', name: 'categories', component: CategoriesList},
{path: '/category/:id', name: 'category.show', component: CategoriesList},
{path: '/cart', name: 'cart', component: Cart},
{path: '/checkout', name: 'checkout', component: Checkout},
{path: '/success', name: 'order_created', component: OrderCreated},
{path: '/search', name: 'search', component: Search},
{path: '/account', name: 'account', component: Account},
];
export const router = createRouter({
history: createWebHashHistory('/image/catalog/tgshopspa/'),
routes,
scrollBehavior(to, from, savedPosition) {
// Для страницы товара всегда скроллим наверх мгновенно
if (to.name === 'product.show') {
return {top: 0, behavior: 'instant'};
}
// Для страницы категории скролл будет восстановлен в компоненте через onActivated
// Здесь просто предотвращаем автоматический скролл наверх
if (to.name === 'product.categories.show') {
// Если возвращаемся назад на категорию - используем savedPosition
if (savedPosition) {
return savedPosition;
}
return false; // Не скроллить автоматически
}
// Для остальных страниц используем savedPosition если есть, иначе наверх
if (savedPosition) {
return savedPosition;
}
return {top: 0, behavior: 'smooth'};
},
});
router.beforeEach((to, from, next) => {
const ym = useYaMetrikaStore();
ym.prevPath = from.path;
next();
});