vue点击跳转路由跳到对应页面位置

本文介绍了如何在Vue应用中利用a标签和ID原理,实现点击按钮后不仅跳转到新的路由,还能将页面滚动到对应的位置。
摘要由CSDN通过智能技术生成

差不多就是a标签,id原理
直接上代码

  • 要点击跳转的页面
<!-- index.vue -->
<div>
   <div @click="about('introduce')">公司简介</div>
   <div @click="about('culture')">公司文化</div>
   
要实现在Vue项目中点击跳转到指定网站,有两种方法可以选择。第一种方法是使用Vue Router,第二种方法是使用window.location.href属性。 使用Vue Router的方法可以使页面跳转更加流畅和灵活,但需要在项目中集成Vue Router。如果您已经在项目中使用了Vue Router,那么您可以简单地在template中添加一个button或者a标签,并绑定点击事件来实现页面跳转。具体操作如下: 1. 在Vue组件中,导入Vue Router库并使用Vue.use()来启用Vue Router。 2. 创建一个Vue Router实例,并设置路由路径和对应的组件。 3. 在template中添加一个button或者a标签,并绑定一个点击事件。 4. 在绑定的点击事件中,使用router.push()方法来实现页面跳转。例如: ``` <template> <div> <button @click="goToOtherSite()">跳到百度</button> </div> </template> <script> import Vue from 'vue' import VueRouter from 'vue-router' Vue.use(VueRouter) const router = new VueRouter({ routes: [ { path: '/other', component: OtherSite } ] }) export default { name: 'Example', methods: { goToOtherSite() { router.push('/other') } } } </script> ``` 使用window.location.href属性的方法相对简单,但是需要注意的是,这种方法只能实现简单的跳转,无法像Vue Router一样灵活地控制页面跳转。具体操作如下: 1. 在template中添加一个button或者a标签,并绑定一个点击事件。 2. 在绑定的点击事件中,使用window.location.href属性来设置跳转的网址。例如: ``` <template> <div> <button @click="goToOtherSite()">跳到百度</button> </div> </template> <script> export default { name: 'Example', methods: { goToOtherSite() { window.location.href = 'https://www.baidu.com' } } } </script> ``` 需要注意的是,如果您的Vue项目使用了路由守卫来拦截路由跳转,那么在使用window.location.href属性进行跳转时,也需要进行相应的配置,以避免跳转异常。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值