当前位置: 首页 > 知识库问答 >
问题:

vue3 - Vue3中的onBeforeEnter未触发原因?

蒯慈
2023-12-29

问一个vue3 setup语法糖中使用onBeforeEnter的问题

<script lang="ts"> //使用接口继承ComponentPublicInstance 增加getSOME(指定)方法interface VmInstance extends ComponentPublicInstance {    getSOME(): void}export default defineComponent({    beforeRouteEnter(to, from, next) {        next((vm) => {            //将vm赋值 as 新的类型            const instance = vm as VmInstance            console.log(from)//获取form判断路由来源            //调用指定的方法或者根据需要进行逻辑判断操作            instance.getSOME()        })    }})// beforeRouteEnter</script><script lang="ts" setup>//引入defineComponent,ComponentPublicInstanceimport { defineComponent, ComponentPublicInstance } from "vue"const getSOME = () => {   //进行某种操作}defineExpose({ getSOME })</script>

这段代码是网上搜索的。但是在项目中没有进入beforeRouteEnter,不知道是什么问题。项目中这个路由是有keep-alive缓存的。或者大家有没有更好的方法,在vue3中监听到其他路由跳转到本组件

解决问题

共有1个答案

杜晨朗
2023-12-29

onBeforeRouteUpdate需要放在全局, 比如#app中, 放在一般组件发生路由时还没注册这个方法呢, 就不会监听到

 类似资料:
  • vue3 新的 defineModel parent: <Input v-model:p_input="test_input" /> son: <el-input v-model="myInput" placeholder="Please input" /> 报警告: Extraneous non-emits event listeners (refresh) were passed to com

  • 可是官方最新的文档里还有这个指令的,文心靠不靠谱啊

  • reactive 收集依赖不是要通过 Proxy 触发 get 吗,为什么这里只是打印 obj(没有触发 get),也能够收集到依赖?

  • VUE3-MUSIC 是一个基于 VUE3+TS 开发的音乐播放器,界面模仿 QQ 音乐 mac 客户端。 VUE3默认版本了,搞个项目熟悉一下,以备不时之需 在线体验:https://smallruraldog.github.io/vue3-music github:https://github.com/SmallRuralDog/vue3-music 将浏览器大小设置成1050*670,体验效

  • 主要内容:GET 方法,GET 实例,GET 实例,传递参数说明,POST 方法,POST 实例,传递参数说明,执行多个并发请求,实例,axios API,实例Vue 版本推荐使用 axios 来完成 ajax 请求。 Axios 是一个基于 Promise 的 HTTP 库,可以用在浏览器和 node.js 中。 Github开源地址: https://github.com/axios/axios 安装方法 使用 cdn: 或 使用 npm: 使用 bower: 使用 yarn: 使用方法:

  • 主要内容:实例,选项合并,实例,实例,全局混入,实例混入 (mixins)定义了一部分可复用的方法或者计算属性。混入对象可以包含任意组件选项。当组件使用混入对象时,所有混入对象的选项将被混入该组件本身的选项。 来看一个简单的实例: 实例 // 定义混入对象 const myMixin = { created() { this.hello() }, methods: { hello() { console.log('欢迎来到混入实例-RUNOOB!'