链接 router-link 和 scrollBehavior 不起作用 - Vue 3 和 Ionic 6
Linking router-link and scrollBehavior not working - Vue 3 and Ionic 6
我从侧面菜单导航到主页上的特定 link 无法使用 ion-router-link
和 scrollBehavior
router/index.js
const routes = [
...
{
path: "/books",
component: Books,
},
{
path: "/books/:key",
component: () => import("../views/BooksDetails.vue"),
},
];
...
const router = createRouter({
history: createWebHistory(process.env.BASE_URL),
routes,
scrollBehavior(to, from, savedPosition) {
if (to.hash) {
return { el: to.hash, left: 0, top: 64 };
}
},
Link <router-link :to="`/books/${selectedBook}#${chapter.key}`"> Jump to chapter </router-link>
目标
<div :id="chapter.key">
to
的输出
"fullPath": "/books/myBook1#chapter1",
"hash": "#chapter1",
"query": {},
"path": "/books/myBook1",
"params": { "key": "myBook1" },
"matched": [
{
"path": "/books/:key",
"meta": {},
"props": { "default": false },
"children": [],
"instances": { "default": {} },
"leaveGuards": {},
"updateGuards": {},
"enterCallbacks": {},
"components": {
"default": {
"props": {},
"__file": "BooksDetails.vue",
"__hmrId": "42539a20"
}
}
}
],
"meta": {},
"href": "/books/myBook1#chapter1"
}
我能够通过 answer from @Masoud Ehteshami to the similar question 解决这个问题。
const router = createRouter({
history: createWebHistory(process.env.BASE_URL),
routes,
scrollBehavior(to, from, SavedPosition) {
if (to.hash) {
const el = window.location.href.split("#")[1];
if (el.length) {
document.getElementById(el).scrollIntoView({ behavior: "smooth" });
}
} else if (SavedPosition) {
return SavedPosition;
} else {
document.getElementById("app").scrollIntoView({ behavior: "smooth" });
}
},
});
我从侧面菜单导航到主页上的特定 link 无法使用 ion-router-link
和 scrollBehavior
router/index.js
const routes = [
...
{
path: "/books",
component: Books,
},
{
path: "/books/:key",
component: () => import("../views/BooksDetails.vue"),
},
];
...
const router = createRouter({
history: createWebHistory(process.env.BASE_URL),
routes,
scrollBehavior(to, from, savedPosition) {
if (to.hash) {
return { el: to.hash, left: 0, top: 64 };
}
},
Link <router-link :to="`/books/${selectedBook}#${chapter.key}`"> Jump to chapter </router-link>
目标
<div :id="chapter.key">
to
"fullPath": "/books/myBook1#chapter1",
"hash": "#chapter1",
"query": {},
"path": "/books/myBook1",
"params": { "key": "myBook1" },
"matched": [
{
"path": "/books/:key",
"meta": {},
"props": { "default": false },
"children": [],
"instances": { "default": {} },
"leaveGuards": {},
"updateGuards": {},
"enterCallbacks": {},
"components": {
"default": {
"props": {},
"__file": "BooksDetails.vue",
"__hmrId": "42539a20"
}
}
}
],
"meta": {},
"href": "/books/myBook1#chapter1"
}
我能够通过 answer from @Masoud Ehteshami to the similar question 解决这个问题。
const router = createRouter({
history: createWebHistory(process.env.BASE_URL),
routes,
scrollBehavior(to, from, SavedPosition) {
if (to.hash) {
const el = window.location.href.split("#")[1];
if (el.length) {
document.getElementById(el).scrollIntoView({ behavior: "smooth" });
}
} else if (SavedPosition) {
return SavedPosition;
} else {
document.getElementById("app").scrollIntoView({ behavior: "smooth" });
}
},
});