我有Vue 3+vue路由器,用于浏览应用程序中的页面。我想导航到#从HomeView下载从不同的页面和从HomeView it self。,但它不能工作,
我的海军
<li class="nav-item">
<router-link :to="{ path: '/#download' }">
<a class="nav-link me-lg-3">Download</a>
</router-link>
</li>我的HomeView
<section class="bg-gradient-primary-to-secondary" id="download"></section>路线设置:
import { createRouter, createWebHistory, RouteRecordRaw } from "vue-router";
import HomeView from "../views/HomeView.vue";
const routes: Array<RouteRecordRaw> = [
{
path: "/",
name: "home",
component: HomeView,
},
{
path: "/about",
name: "about",
// route level code-splitting
// this generates a separate chunk (about.[hash].js) for this route
// which is lazy-loaded when the route is visited.
component: () =>
import(/* webpackChunkName: "about" */ "@/views/AboutView.vue"),
},
{
path: "/privacy-policy",
name: "PrivacyPolicyView",
// route level code-splitting
// this generates a separate chunk (about.[hash].js) for this route
// which is lazy-loaded when the route is visited.
component: () =>
import(/* webpackChunkName: "about" */ "@/views/PrivacyPolicyView.vue"),
},
];
const router = createRouter({
history: createWebHistory(process.env.BASE_URL),
routes,
scrollBehavior(to) {
if (to.hash) {
return {
selector: to.hash,
} as any;
}
},
});
export default router;发布于 2022-02-21 07:15:43
这段代码适用于我:
const router = createRouter({
history: createWebHistory(process.env.BASE_URL),
routes,
scrollBehavior(to, from, savedPosition) {
if (savedPosition) {
return savedPosition;
} else {
return {
el: to.hash,
behavior: "smooth",
};
}
},
});https://stackoverflow.com/questions/71197754
复制相似问题