next.js 将函数从一个js文件导入到另一个js文件

lrl1mhuk  于 2023-05-22  发布在  其他
关注(0)|答案(1)|浏览(134)

我试图导入功能,因为我想点击<il> servies</il>并滚动到主页中的服务部分
我只是想点击这是在导航栏页面和服务部分是在主页上滚动李。这两个页面是不同的,为什么我的模块不工作的NEXT.JS

代码镜像navbar code imageserviecs page code image
**面对错误 *

Import trace for requested module:
./components/Navbar.js
wait  - compiling...
error - ./components/Navbar.js
Error:
  x Expected ',', got ':'
    ,-[D:\Next.js projects\help-desk\components\Navbar.js:8:1]
  8 |   const ref  = useRef();
  9 |   const scrollToSection = (elementRef) =>{
 10 |     window.scrollTo(
 11 |       top:elementRef.current.offsetTop,
    :          ^
 12 |       behavior: 'smooth'
 13 |     )
 13 |   }
    `----

Caused by:
    Syntax Error

Import trace for requested module:
./components/Navbar.js

navbar.js Code:

import React from 'react'
import Image from 'next/image'
import Link from 'next/link'
import { useRef } from 'react'
import { AiOutlineSearch } from 'react-icons/ai';
AiOutlineSearch
const Navbar = () => {
  const ref  = useRef();
  const scrollToSection = (elementRef) =>{
    window.scrollTo(
      top:elementRef.current.offsetTop,
      behavior: 'smooth'
    )
  }
  return (
    <>
    <div className='flex flex-col md:flex-row md:justify-start justify-center items-center py-1 drop-shadow-xl bg-slate-100'>
    <div className='logo mx-9 cursor-pointer'>
      <Image width={70} height={70} src="/navhome.png" alt=" " />
    </div>
    <div className="nav">
        <ul className='flex items-center space-x-6 font-bold md:text-md'>
          <Link legacyBehavior href={"/"}><a><li>Home</li></a></Link>
          <Link legacyBehavior href={"/about"}><a><li>About</li></a></Link>
          {/* <Link legacyBehavior href={"/servies"}><a><li>Services</li></a></Link> */}
          <li onClick={()=> scrollToSection(ref) href="#section"}>Services</li>
          <Link legacyBehavior href={"/contact"}><a><li>Contact</li></a></Link> 
        </ul>
      </div>
      <div className="search absolute right-0 top-6 mx-5">
       <AiOutlineSearch className='text-xl  md:text-3xl'/>
      </div>
    </div> 
    </> 
  )
}

export default Navbar;
export { scrollToSection };

index.js文件:

import Image from 'next/image'
import Head from 'next/head'
import Link from 'next/link'
import  {scrollToSection}  from '../components/Navbar.js'
import Servies from '../pages/servies'

export default function Home() {
  return (
    <div>
      <Head>
        <title>HelpDesk.com - we're here to serve you </title>
        <meta name="description" content="HelpDesk.com - we're here to serve you" />
        <link rel="icon" href="/favicon.ico" />
      </Head>
      <div>
        <img src="/home.jpg" alt="" />
      </div>
      <section ref={ref} className='scrollService' id='sectionid' >
        <Servies />
        </section>
      <br />
    </div>
    )
}
jdzmm42g

jdzmm42g1#

如果使用节的id属性作为链接的目标,则可以获得所需的结果。

<section id="section"/>

Next.js

<Link href="#section"/>

React

<Link to="#section"/>

HTML

<a href="#section">Scroll to section</a>

相关问题