javascript - Vue this.$router.push 在 setTimeout 中

标签 javascript vue.js vuejs2

我有一个着陆页 '/' 用户访问我们的网站时会点击该页面,然后我有一个加载轮,我想加载 5 秒然后重定向我到登录 '/login' 页面。

我正在使用 Vue 和 Bulma.io,在我的 Landing.vue 页面中我有:

<template>
<div class="image">
  <img src="../assets/landing.png">
  <div class="loader loading"></div>
</div>
</template>


<!-- Styles -->
<style>
  .loading {
    border: 2px solid #dbdbdb;
    border-radius: 290486px;
    border-right-color: transparent;
    border-top-color: transparent;
    content: "";
    display: block;
    height: 1em;
    position: absolute;
    width: 1em;
    transform: translate(-50%,-50%);
    margin-right: -50%;
    top: 30%;
    left: 50%;
  }
</style>

<!-- Scripts -->
<script>
 setTimeout( function() { this.$router.push({ path: '/login'})},5000);
</script>

在我的 router/index.js 中我有:

/*
  Necessary imports...notice I imported the two components we will use( Login and Home)
*/
import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/components/Login'
import Home from '@/components/Home'
import Landing from '@/components/Landing'

// Needed to make use of the vue-router system
Vue.use(Router)


export default new Router({

  // Define the routes...will add more when they are needed
  routes: [
    {
      path: '/home',    // this is the path in the URL
      name: 'Home',
      component: Home   // created component
    },
    {
      path: '/login',   // this is the path in the URL
      name: 'Login',
      component: Login  // created component
    },
    {
      path: '/',      // this is the path in the URL
      name: 'Landing',
      component: Landing // created component
    }
  ]
})

所以我的 < script > 部分中的函数一定有问题,对吧?

感谢您提供的任何帮助。

最佳答案

如果您正在定义一个脚本部分,您需要导出一个实际的 Vue 组件。

<script>

  export default {
    created(){
      setTimeout( () => this.$router.push({ path: '/login'}), 5000);

    }
  }

</script>

关于javascript - Vue this.$router.push 在 setTimeout 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46761019/

相关文章:

javascript - 在 vuejs 中使用条件 inside filter()?

javascript - Vuejs 动态添加 ref undefined

javascript - 如何知道动态组件何时完全加载

javascript - 如何从n个数组中按一定比例选取n个元素?

javascript - 控制台不记录内容脚本中的 js 错误

javascript - angularJS 服务模型避免重复

javascript - VueJS 语法 : Running method on mount

javascript - 使用 useState 时,React 不会在状态更改时重新渲染

css - 单击 Vuejs + Tailwind CSS 时如何更改下拉菜单

html - 异常值 : string indices must be integers : Render bundle error vue + django