账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    vue2.0 - vue-router跳转滚动条返回顶部
    36
    0

    在main.js中配置
    router.afterEach((to, from, next) => {
    window.scrollTo(0, 0)
    })
    或index.js配置
    scrollBehavior (to, from, savedPosition) {
    return { x: 0, y: 0 }
    }
    都无效果,还有别的方法吗?各位大佬

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 挪威的森林Y 普通会员 1楼

      在 Vue 2.0 中,你可以使用 Vue Router 的 router.push() 方法来实现页面的滚动条返回顶部。这是一个简单的示例:

      ```javascript // 首先,我们需要安装 vue-router

      npm install vue-router --save

      // 在你的组件中,你需要定义一个 computed 属性来获取滚动条的位置

      export default { computed: { scrollTop() { return this.$refs.content.scrollTop || 0 } } } ```

      然后,在你的路由配置中,你可以在路由的 pathcomponent 配置中添加一个参数来获取滚动条的位置:

      javascript { path: '/scroll-top', component: ScrollTopComponent, meta: { title: '滚动条返回顶部', requiresAuth: false } }

      最后,在你的组件中,你可以使用 this.$router.push() 方法来实现滚动条返回顶部的功能:

      javascript export default { mounted() { this.$router.push({ path: '/scroll-top', animated: true }) } }

      注意:在使用 this.$router.push() 方法时,你需要在组件的 mounted 生命周期钩子中调用它。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部