Link 链接组件 #

基本用法 #

jsx
import { Link } from '@chakra-ui/react'

function Example() {
  return (
    <Link href="https://chakra-ui.com" isExternal>
      Chakra UI
    </Link>
  )
}

链接样式 #

jsx
<Link color="blue.500">蓝色链接</Link>
<Link color="teal.500">青色链接</Link>
<Link color="purple.500">紫色链接</Link>

外部链接 #

jsx
<Link href="https://google.com" isExternal>
  Google <ExternalLinkIcon mx="2px" />
</Link>

链接变体 #

jsx
<Link variant="underline">下划线链接</Link>
<Link variant="plain">普通链接</Link>

悬停效果 #

jsx
<Link
  href="#"
  color="blue.500"
  _hover={{
    color: 'blue.600',
    textDecoration: 'underline',
  }}
>
  自定义悬停效果
</Link>

实用示例 #

导航链接 #

jsx
function NavLink({ href, children, isActive }) {
  return (
    <Link
      href={href}
      px={3}
      py={2}
      borderRadius="md"
      color={isActive ? 'blue.500' : 'gray.600'}
      bg={isActive ? 'blue.50' : 'transparent'}
      _hover={{
        bg: 'blue.50',
        color: 'blue.500',
      }}
    >
      {children}
    </Link>
  )
}

链接列表 #

jsx
function LinkList({ links }) {
  return (
    <HStack spacing={4}>
      {links.map((link) => (
        <Link
          key={link.href}
          href={link.href}
          color="gray.600"
          _hover={{ color: 'blue.500' }}
        >
          {link.label}
        </Link>
      ))}
    </HStack>
  )
}

与 React Router 集成 #

jsx
import { Link as RouterLink } from 'react-router-dom'

<Link as={RouterLink} to="/about">
  关于我们
</Link>
属性 说明
href 链接地址 URL
isExternal 外部链接 boolean
variant 变体 underline, plain
color 颜色 颜色值

下一步 #

现在你已经掌握了 Link 组件,接下来学习 主题定制,了解如何自定义 Chakra UI 主题!

最后更新:2026-03-28