@KushchO

Как по таймеру изменить route во Vue.js?

Я только приступил к изучению vue.js. У меня возник вопрос, есть ли способ с помощью например setInterval() менять routes?
  • Вопрос задан
  • 259 просмотров
Решения вопроса 2
Lumore
@Lumore
Front-end developer
export default {
  created () {
    setTimeout(() => {
      this.$router.push('/contacts')
    }, 5000)
  }
}
Ответ написан
Комментировать
0xD34F
@0xD34F Куратор тега Vue.js
const router = new VueRouter({
  routes: [
    { path: '/',    component: { template: '<div>Home</div>' } },
    { path: '/foo', component: { template: '<div>Foo</div>'  } },
    { path: '/boo', component: { template: '<div>Boo</div>'  } },
  ],
});

new Vue({
  router,
  el: '#app',
  data: () => ({
    routeIndex: 0,
  }),
  created() {
    setInterval(() => {
      const { routes } = this.$router.options;
      this.routeIndex = (this.routeIndex + 1) % routes.length;
      this.$router.push(routes[this.routeIndex]);
    }, 1000);
  },
});

<div id="app">
  <router-view></router-view>
</div>
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы