简体   繁体   English

当我在值更改观察程序中通过 $router.push 导航时,Vue scrollBehavior 不起作用

[英]Vue scrollBehavior doesn't work when I navigate by $router.push in a value changing watcher

When I navigate to another page by routerLink or $router.push scrollBehavior works fine.当我通过 routerLink 或 $router.push 导航到另一个页面时,滚动行为工作正常。 But when I try to navigate to the same page by a watcher which is triggered by changing input value of an input box situated in the center of the home page, scrollBehavior doesn't work.但是,当我尝试通过更改位于主页中心的输入框的输入值触发的观察者导航到同一页面时,scrollBehavior 不起作用。 Rather it gets the position at bottom of the new navigated page.而是在新导航页面的底部获得 position。 Even if I don't use scrollBehavior it does the same.即使我不使用 scrollBehavior 它也是一样的。

Now my question is how can I get to the top of the navigated 'About' page by the input value changing watcher?现在我的问题是如何通过输入值更改观察程序到达导航的“关于”页面的顶部?

HeadSearch Component:头部搜索组件:

<template>
  <div class="search">
    <input type="text" v-model="tx" />
    <p>{{ tx }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tx: "",
    };
  },
  watch: {
    tx() {
      if (this.$route.path !== "/about") {
        this.$router.replace("/about");
      }
    },
  },
};
</script>

App.vue:应用程序.vue:

<template>
  <div id="app">
    <div id="nav">
      <router-link to="/">Home</router-link> |
      <router-link to="/about">About</router-link>
      <br /><br />
      <head-search></head-search>
      <br /><br />
    </div>
    <router-view />
  </div>
</template>

<script>
import HeadSearch from "./components/HeadSearch.vue";
export default {
  components: {
    HeadSearch,
  },
};
</script>

Home.vue:主页.vue:

<template>
  <div class="home">
    <h1>This is Home page</h1>
    <br /><br /><br />
    <input class="home-input" type="text" v-model="hTx" />
  </div>
</template>

<script>
export default {
  name: "Home",
  data() {
    return {
      hTx: "",
    };
  },
  watch: {
    hTx() {
      if (this.$route.path !== "/about") {
        this.$router.push("/about");
      }
    },
  },
};
</script>

About.vue:关于.vue:

<template>
  <div class="about">
    <h1>This is an about page</h1>
    <br /><br /><br /><br /><br />
    <div v-for="(cont, index) in 30" :key="index">
      {{ cont }} {{ index }}
      <p>Lorem ipsum dolor sit amet.</p>
    </div>
  </div>
</template>

router.js:路由器.js:

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
  scrollBehavior(){
    return {
      x: 0,
      y: 0
    }
  }
})

You can define a scrollTop or scrollIntoView using Javascript or jQuery in your About component methods and call the method in your About component's mounted hook.您可以在About组件方法中使用 Javascript 或 jQuery 定义scrollTopscrollIntoView ,并在About组件的挂载钩子中调用该方法。

Using Javascript or jQuery, you could define an id , class or element to scroll to when the About component is mounted.使用 Javascript 或 jQuery,您可以定义一个idclass或在安装About组件时要滚动到的元素。

eg id="scrollTarget"例如id="scrollTarget"

<template>
    <div class="about" id="scrollTarget">
        <h1>This is an about page</h1>
        <br /><br /><br /><br /><br />
        <div v-for="(cont, index) in 30" :key="index">
            {{ cont }} {{ index }}
            <p>Lorem ipsum dolor sit amet.</p>
        </div>
    </div>
</template>


<script>
export default {
    methods: {
        scrollToTop() {
            // using Javascript
            document.getElementById("scrollTo").scrollIntoView({ behavior: "smooth" })
            // OR
            // using jQuery
            $("html, body").animate({scrollTop: 600}, "slow")
        }
    },
    mounted() {
        this.scrollToTop();
    }
}
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM