vue router.push:TypeError:onComplete不是函数

艾莉·萨迪琴(Allie Syadiqin)

多年使用CodeIgniter之后,我对VueJS + Laravel还是陌生的,并试图了解Laravel + VueJS是如何工作的。无论如何,我得到了一个运行有Vue加载层包的测试页,该包在onClick事件上触发了加载叠加层,该事件使用router.push加载了新页面。我阅读了文档,其中有2个其他参数。用于router.push的onComplete和onAbort,并提出了以下方法。一切正常,除了控制台出现错误。

控制台错误

[vue-router] uncaught error during route navigation:
TypeError: onComplete is not a function

转到方法

goTo(routeName) {
    let self = this
    self.$nextTick( function() {

        // Show Vue Loading Layer
        let loader = this.$loading.show({
            loader: 'spinner',
            color: '#e8b30f',
            backgroundColor: '#000',
            opacity: 0.5,
        })

        // Retrieve new page
        self.$router.push(

            // First param : location
            {name: routeName},

            // Second param : onComplete
            setTimeout(() => loader.hide(), 3 * 1000)
        )
    })
}

即使我得到了预期的结果,我仍然想知道为什么该错误出现在控制台上。

第二,是否有更好的方法?我使用setTimeout()的唯一原因是新页面的加载速度太快,无法正确设置加载叠加层,而没有设置setTimeout()。

感谢您的帮助或提示。

阿比舍克·古普塔

.push期望功能的第二个参数,您传递的是

setTimeout(() => loader.hide(), 3 * 1000)

返回整数,如您在此处所读:https : //developer.mozilla.org/zh-CN/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout#Return_value

要修复它,您可以将其包装在一个函数中

() => {
  setTimeout(() => loader.hide(), 3 * 1000)
}

所以你的goto方法看起来像

goTo(routeName) {
    let self = this
    self.$nextTick( function() {

        // Show Vue Loading Layer
        let loader = this.$loading.show({
            loader: 'spinner',
            color: '#e8b30f',
            backgroundColor: '#000',
            opacity: 0.5,
        })

        // Retrieve new page
        self.$router.push(

            // First param : location
            {name: routeName},

            // Second param : onComplete
            () => setTimeout(() => loader.hide(), 3 * 1000)
        )
    })
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Vue-Router:TypeError:this._router.init不是函数

Vue this。$ router.push在setTimeout

vue-router不是呈现组件,而是在此上更改URL。$ router.push

Vue测试-'... .push不是函数'

Vue TypeError _vm不是函数

TypeError:this.router.createUrlTree不是函数

Vue test-utils如何测试router.push()

Vue Router Push 如何设置可选的查询参数?

渲染函数中的错误:“TypeError:无法读取未定义的属性”,使用 Vuex 和 Vue Router

Vue-router 组件内守卫不会触发 router.push() 被调用

将自定义数据传递到vue-router中的$ router.push()

vue-router中的next()保护函数如何工作?

vue-router beforeEach函数不返回已更新的项目

Vue Router beforeEnter与beforeEach

Vue.JS“ TypeError:reverseMessage不是函数”

TypeError:Object(...)不是Vue中的函数

Vue:TypeError:_vm.myFunction不是函数

Vuetify - TypeError:Vue.observable 不是函数

Vue CLI + Axios TypeError:XX 不是函数

React-Router v4。如何将参数附加到URL?TypeError:history.push不是函数

Vue.js:在vue.router路由内使用mixin函数

TypeError:this。$ route.push不是函数vue.js,并且未被捕获TypeError:无法读取未定义的属性“ push”

Vue Router - 路径被多次连接

Vue 3:TypeError:编译器。函数不是函数

router.navigate() 不是函数

Vue.js表单需要刷新才能使router.push工作

Vue。$ router.push具有相同的路由但参数不同-如何触发转换?

如何在Vue.js的$ router.push中传递数据?

VUE路由器:更新查询时this。$ router.push不起作用