B5net

人生是场无尽旅途,欢声笑语,踟蹰彷徨,走过的是岁月,路过的是迷茫。向前,是希望极光;回首,是悠长深巷。

Vue3 路由监听watch 和 computed

Published on:2022-05-09

格式,demo需要是ref/reactive响应式类型的


watch(demo, () => {})
watch(() => demo.name, () => {})


监听路由方式


1.router


import { watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
const router = useRouter()
//监听路由变化
watch(router.currentRoute, (newRouter, oldRouter) => {
    console.log(newRouter.path)
})


2.route


import { watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
const route = useRoute()
//console.log(route)
// {path: ComputedRefImpl, name: ComputedRefImpl, params: ComputedRefImpl, query: ComputedRefImpl}
//可以监听 route.path,route.params route.query等等,方式如下
watch(
    () => route.params,
    (newPath, oldPath) => {
        console.log(newPath)
    },
    {
        immediate: true, //是否首次进行监听,默认false
        deep: true //是否开启深度监听
    }
)



computed

const test = ref(1)

const newTest = computed(() => {
    return test.value + 10
})
//直接箭头函数,等价于下面
const newTest2 = computed({
    get() {
        return test.value + 10
    }
})
//设置 newTest2.value =xxx 不会修改test的值

//还可以设置set
const newTest3 = computed({
    get() {
        return test.value + 10
    },
    set(value) {
        //自己根据需要处理
        test.value = value - 10
    }
})

//直接计算 store的值
const store = useStore()
const menList = computed(() => store.state.menuList)



留言列表(0)

    留言

    B5net

    人生是场无尽旅途,欢声笑语,踟蹰彷徨,走过的是岁月,路过的是迷茫。向前,是希望极光;回首,是悠长深巷。

    开源项目
    最新评论
    小白 :大佬您好 请问一下 http://b5laravelcmf.b5net.com/admin这个演示网址的全部代码有吗,gitee上不全呢,还能提供一下吗,感激不尽阿
    tz :大佬 B5YiiCMF 还开放吗
    weifox on GoLang常用的三方库 :还有 https://github.com/golang-module/carbon
    php :关于导出功能:1、B5thinkCMF部署后 参数的导出功能点击后就白屏了,不知道是哪里有问题?2、人员部门的导出功能没看懂怎么配置的? 只是启用exportshow=true吗 方便的话请答复下,谢谢!
    初学者 :您好! 部署了您的B5ThinkCMF,请教下 使用新增功能如何实现二级联动呢 ?谢谢
    11 :22
    pcy :前排围观
    Louis :冰舞的博客使用yii框架做的吧,B5ThinkCMF在本地部署后,登录系统的响应时间要比B5YiiCMF的登录时间长,还有一些Bug待修复
    34 :感谢你的开源项目
    文章分类