当前位置: 首页 > wzjs >正文

表白代码青岛网站seo公司

表白代码,青岛网站seo公司,网站开发与服务合同,静态网页多少钱一页目录 前言1. unref2. Demo 前言 从实战中学习,了解一点点知识点 unref 主要用于解包 ref,特别是在 Vue Router 4 里,currentRoute 是一个响应式 ref,需要 .value 或 unref 来访问具体字段 1. unref unref 是 Vue 3 提供的工具函…

目录

  • 前言
  • 1. unref
  • 2. Demo

前言

从实战中学习,了解一点点知识点

在这里插入图片描述

unref 主要用于解包 ref,特别是在 Vue Router 4 里,currentRoute 是一个响应式 ref,需要 .value 或 unref 来访问具体字段

1. unref

unref 是 Vue 3 提供的工具函数,可以解包 ref 值,即:

  • 如果传入的是 ref,返回 ref.value
  • 如果传入的不是 ref,直接返回原值
import { ref, unref } from 'vue'const count = ref(10)console.log(count.value) // 10
console.log(unref(count)) // 10console.log(unref('Hello Vue 3')) // 'Hello Vue 3' (非 ref 直接返回)

再者为什么 currentRoute 需要 unref?
为何要用unref来解析呢

在 Vue Router 4 里,useRouter().currentRoute 是一个 ref 对象,用于存储当前路由信息

const router = useRouter()
console.log(router.currentRoute) // 这是一个 ref 对象
console.log(router.currentRoute.value) // 访问当前路由信息

由于 currentRoute 是 ref,所以要获取 name、path 等字段时,可以使用:

const { name } = router.currentRoute.value

或者:

const { name } = unref(router.currentRoute)

它的实际应用放在监听当前路由变化,或者访问 name、path、meta 等字段

import { unref } from 'vue'
import { useRouter } from 'vue-router'const { currentRoute } = useRouter()console.log(unref(currentRoute)) // 打印完整的当前路由对象
console.log(unref(currentRoute).name) // 获取路由 name

2. Demo

具体小例子:

<script setup lang="ts">
import { ref, watch, onMounted, unref } from 'vue'
import { useRouter, useRoute } from 'vue-router'const router = useRouter()
const route = useRoute()const routeName = ref('')
const routePath = ref('')// 监听 currentRoute 变化
watch(() => route.name, (newName) => {routeName.value = newName as stringconsole.log('当前路由 name:', newName)
})watch(() => route.path, (newPath) => {routePath.value = newPathconsole.log('当前路由 path:', newPath)
})// 在 mounted 阶段输出初始值
onMounted(() => {console.log('完整的路由对象:', unref(router.currentRoute))console.log('当前路由 name:', unref(router.currentRoute).name)console.log('当前路由 path:', unref(router.currentRoute).path)
})
</script><template><div><h2>当前路由信息</h2><p>路由名称: {{ routeName }}</p><p>路由路径: {{ routePath }}</p></div>
</template>
http://www.dtcms.com/wzjs/355458.html

相关文章:

  • 江门网站建设公司哪家好百度搜索引擎广告投放
  • 网站 标题 字体企业培训方案制定
  • 专业的论坛网站建设开发今日实时热搜
  • 什么是网站什么是网站建设itme收录优美图片官网
  • wordpress. 外贸seo百度地图优化
  • 武汉网站建设百家号加盟
  • 网页设计毕业论文免费广州网站优化方式
  • 沈阳响应式网站制作营销推广活动策划
  • 运城建设银行网站推广网站的文案
  • 中国海员建设工会全国委员会网站成都移动seo
  • 长春火车站电话网站优化排名哪家性价比高
  • 汕头市住建局东莞优化怎么做seo
  • 建设ipv6网站软文广告经典案例300
  • ps做的网站模板品牌策划方案怎么写
  • 深圳做网站专业网盘资源
  • wordpress提交评论慢seo关键词排名优化是什么
  • 网站一键制作淄博网站制作优化
  • 南京做网站牛企业如何开展网络营销
  • 荣耀商城seo职业
  • 北京移动官方网站百度推广开户费用多少
  • 亚马逊卖家可以做促销的网站兴安盟新百度县seo快速排名
  • wordpress p=优化建站
  • 石家庄做外贸网站建设全网推广代理
  • 石家庄有没有销售做被用的网站中国企业500强最新排名
  • 建站公司杭州无锡百度快速优化排名
  • 做爰网站美女应用商店aso
  • 四平网站建设公司十大营销案例分析
  • 广州网站制作功能百度搜索量查询
  • wordpress维护汕头网站优化
  • 网站建设一般要素营销案例网站