elementui-plus 锚点
时间: 2025-01-23 09:10:15 浏览: 65
### 关于Element Plus 中锚点组件的使用
对于 Vue 3.0 的项目来说,确实应当采用 Element Plus 而不是旧版的 Element UI[^1]。针对希望了解如何在 Element Plus 中运用锚点(Anchor)组件的需求,以下是具体指导。
#### 安装依赖包
为了能够正常使用 Element Plus 及其 Anchor 组件,在项目的根目录下执行如下命令来安装必要的库:
```bash
npm install element-plus --save
```
#### 导入并注册全局组件
接着修改 `main.js` 文件以确保正确加载了 Element Plus 和样式文件:
```javascript
import { createApp } from 'vue'
import App from './App.vue'
// ...其他导入语句...
import ElementPlus from 'element-plus';
import 'element-plus/dist/index.css';
const app = createApp(App);
app.use(ElementPlus);
app.mount('#app');
```
#### 使用Anchor组件实例
下面是一个简单的例子展示怎样利用 `<el-anchor>` 来创建带有跳转功能的内容结构:
```html
<template>
<div class="example">
<!-- 锚点菜单 -->
<el-anchor :offset="200" style="margin-1">第一章</el-link><br/>
<el-link href="#chapter-2">第二章</el-link><br/>
<el-link href="#chapter-3">第三章</el-link>
</el-anchor>
<!-- 实际章节内容 -->
<section id="chapter-1"><h2>第一章 标题</h2></section>
<p v-for="(item, index) in Array(5)" :key="index">{{ item }}</p>
<section id="chapter-2"><h2>第二章 标题</h2></section>
<p v-for="(item, index) in Array(5)" :key="index">{{ item }}</p>
<section id="chapter-3"><h2>第三章 标题</h2></section>
<p v-for="(item, index) in Array(5)" :key="index">{{ item }}</p>
</div>
</template>
```
此模板定义了一个侧边栏形式的导航条以及三个不同部分的文章主体。当点击左侧链接时,页面会平滑滚动至相应位置,并高亮显示当前激活项。
阅读全文
相关推荐

















