我可以滚动到我的目标网页的特定部分时,直接访问它通过URL [Nextjs]

uqdfh47h  于 2023-06-22  发布在  其他
关注(0)|答案(1)|浏览(122)

我想做的是导航到特定的部分,在着陆页时,用户直接访问它的URL,即(localhost:3000/常见问题解答)
我尝试使用了许多解决方案:但它总是重定向到404页面

  1. import DefaultLayout from "@Components/DefaultLayout";
  2. import {
  3. JoinCommunity,
  4. LandingBanner,
  5. MixMatch,
  6. OnlinePresence,
  7. LandingFaqs,
  8. } from "@Components/LandingPage";
  9. import { NextPage } from "next";
  10. import { RIA } from "@Constants/acl";
  11. import { useRouter } from "next/router";
  12. import useTranslation from "next-translate/useTranslation";
  13. import withGuard from "@Hocs/withGuard";
  14. import { SocialProfileJsonLd } from "next-seo";
  15. import { generatePageSeo, getFullURL } from "@Utils/Seo";
  16. interface Props {
  17. ACL: {
  18. action: string;
  19. };
  20. }
  21. const LandingPage: NextPage<Props> = () => {
  22. const { locale, asPath } = useRouter();
  23. const { t } = useTranslation("common");
  24. const completeURL = getFullURL(asPath, locale);
  25. return (
  26. <DefaultLayout
  27. showFooter
  28. showLandingNav
  29. fullWidth
  30. >
  31. <LandingBanner />
  32. <OnlinePresence />
  33. <MixMatch />
  34. <JoinCommunity />
  35. <LandingFaqs />
  36. </DefaultLayout>
  37. );
  38. };
  39. LandingPage.getInitialProps = async () => {
  40. return {
  41. ACL: {
  42. action: RIA,
  43. },
  44. };
  45. };
  46. export default withGuard(LandingPage);

所以我想知道有没有办法做到这一点

djp7away

djp7away1#

经过思考我的问题,我认为我是过度工程的解决方案,所以我做了什么我创建了另一个页面称为faqs下的页面目录和它的内容是相同的着陆页和执行滚动行为

  1. import DefaultLayout from "@Components/DefaultLayout";
  2. import {
  3. JoinCommunity,
  4. LandingBanner,
  5. MixMatch,
  6. OnlinePresence,
  7. LandingFaqs,
  8. } from "@Components/LandingPage";
  9. import { NextPage } from "next";
  10. import { RIA } from "@Constants/acl";
  11. import { useRouter } from "next/router";
  12. import useTranslation from "next-translate/useTranslation";
  13. import withGuard from "@Hocs/withGuard";
  14. import { SocialProfileJsonLd } from "next-seo";
  15. import { generatePageSeo, getFullURL } from "@Utils/Seo";
  16. import { createRef, useEffect, useLayoutEffect, useRef } from "react";
  17. interface Props {
  18. ACL: {
  19. action: string;
  20. };
  21. }
  22. const LandingPage: NextPage<Props> = () => {
  23. const { locale, asPath } = useRouter();
  24. const { t } = useTranslation("common");
  25. const completeURL = getFullURL(asPath, locale);
  26. const faqsRef = useRef(null);
  27. useLayoutEffect(() => {
  28. if (faqsRef.current) {
  29. console.log("scrolling to faqs", faqsRef.current);
  30. faqsRef.current.scrollIntoView({ behavior: "smooth", block: "start", inline: "nearest" });
  31. }
  32. }, []);
  33. return (
  34. <DefaultLayout
  35. showFooter
  36. showLandingNav
  37. fullWidth
  38. seoData={generatePageSeo(
  39. t("seo-landing-title"),
  40. locale,
  41. asPath,
  42. `${t("reach-link-head")} | %s`,
  43. t("seo-landing-desc"),
  44. )}
  45. >
  46. <LandingBanner />
  47. <OnlinePresence />
  48. <MixMatch />
  49. <JoinCommunity />
  50. <div ref={faqsRef}>
  51. <LandingFaqs />
  52. </div>
  53. </DefaultLayout>
  54. );
  55. };
  56. LandingPage.getInitialProps = async () => {
  57. return {
  58. ACL: {
  59. action: RIA,
  60. },
  61. };
  62. };
  63. export default withGuard(LandingPage);
展开查看全部

相关问题