export default {
mounted() {
setTimeout(function() {
this.$emit('onLoad')
}, 4000);
}
} //views/Load.vue
我想在页面访问后 4 秒时移动到另一个页面。
<template>
<div id="app">
<transition name="fade" mode="out-in">
<router-view
@onLoad="changeRoute('login')">
</router-view>
</transition>
</div>
</template>
<script>
export default {
name: 'app',
methods: {
changeRoute (routeName) {
this.$router.push({ name: routeName })
}
}
}
</script> //App.vue
我想通过'$emit'向'App.vue'发送信号,然后转到通过路由器连接的页面。
所有路由器均已正确连接。
但是出现了“Uncaught TypeError: this.$emit is not a function”的错误。
我该怎么办?
最佳答案
看起来问题可能是 setTimeout
内的函数未绑定(bind)到上下文,因此您可以尝试使用箭头函数或 bind外部上下文的函数:
export default {
mounted() {
setTimeout(() => {
this.$emit('onLoad')
}, 4000);
}
}
关于javascript - vue.js 未捕获类型错误 : this. $emit 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59589821/