Vue el-menu-item实现路由跳转
时间: 2023-08-29 12:13:52 浏览: 48
Vue el-menu-item组件可以通过绑定@click事件来实现路由跳转。
示例代码:
```html
<el-menu-item index="/home" @click="goto('/home')">首页</el-menu-item>
```
```javascript
methods: {
goto(path) {
this.$router.push({ path: path })
}
}
```
在el-menu-item组件上绑定@click事件,当点击该菜单项时,执行goto方法。goto方法接收一个路由路径参数,使用Vue Router的push方法进行路由跳转。
相关问题
el-menu-item怎么配置路由跳转
在 Element UI 中,可以使用 `el-menu-item` 组件来实现菜单项,并使用 `router-link` 组件来实现路由跳转。具体的实现方式如下:
1. 在 Vue Router 中定义路由映射,将菜单项的路径与具体的组件进行关联。
2. 在模板中使用 `el-menu-item` 组件来生成菜单项,并使用 `router-link` 组件来实现路由跳转。
示例代码如下:
```html
<template>
<div>
<el-menu default-active="home" mode="horizontal">
<el-menu-item index="home">
<router-link to="/">首页</router-link>
</el-menu-item>
<el-menu-item index="about">
<router-link to="/about">关于我们</router-link>
</el-menu-item>
<el-menu-item index="contact">
<router-link to="/contact">联系我们</router-link>
</el-menu-item>
</el-menu>
<router-view></router-view>
</div>
</template>
<script>
export default {
// ...
};
</script>
```
在上面的示例中,使用 `el-menu` 组件来生成菜单,使用 `router-link` 组件来实现路由跳转。在 `el-menu-item` 组件中,使用 `index` 属性来指定菜单项的唯一标识符,并将其与路由路径进行关联。在 `router-link` 组件中,使用 `to` 属性来指定跳转的路由路径。在组件的模板中使用 `router-view` 组件来显示当前路由匹配的组件。
el-menu-item 跳转到路由上
el-menu-item 是 Element UI 中用于创建导航菜单的组件,它可以实现跳转到路由上的功能,具体操作如下:
1. 首先,需要在 Vue Router 中定义路由,例如:将路径为“/home”的路由与组件 Home 绑定
```
const routes = [
{ path: '/home', component: Home }
]
```
2. 在 el-menu 中使用 el-menu-item,将它绑定到路由上,例如:
```
<el-menu>
<el-menu-item index="/home" route>
<i class="el-icon-menu"></i>
<span>首页</span>
</el-menu-item>
</el-menu>
```
其中,index 属性是菜单项的唯一标识,route 属性用于将菜单项设置为路由链接。
3. 在组件中,需要使用 router-link 组件来创建路由链接,例如:
```
<router-link to="/home">返回首页</router-link>
```
这样,当用户点击 el-menu-item 中的“首页”菜单项时,就会跳转到路径为“/home”的路由,同时显示组件 Home。在组件中,使用 router-link 组件可以直接在页面中创建一个路由链接,为用户提供更加友好的体验。
总之,使用 el-menu-item 跳转到路由上可以实现页面导航的功能,同时结合 Vue Router 和 router-link 组件,使用户可以更加方便地浏览网站的不同页面。