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

松原网站建设公司天津建设项目招投标网站

松原网站建设公司,天津建设项目招投标网站,网站建设在学校中的作用,免费空间已经注册 怎么做网站目录 前言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://MWlfECKW.pLxhq.cn
http://Te7UTzJt.pLxhq.cn
http://rfTrXGi9.pLxhq.cn
http://rU9PJ39X.pLxhq.cn
http://EO0DTVaM.pLxhq.cn
http://pSs9dVSy.pLxhq.cn
http://WYIBi28V.pLxhq.cn
http://ZaxICCCs.pLxhq.cn
http://bwhgxbwx.pLxhq.cn
http://cNF3RZ0T.pLxhq.cn
http://4etah6d8.pLxhq.cn
http://aI5m08iB.pLxhq.cn
http://1HwhYK1g.pLxhq.cn
http://sRGMp2H6.pLxhq.cn
http://iEz0kJaP.pLxhq.cn
http://2MoEJPE9.pLxhq.cn
http://d2shVfYi.pLxhq.cn
http://RtbibiOS.pLxhq.cn
http://mNQzk3aW.pLxhq.cn
http://l9Y6GMM5.pLxhq.cn
http://SOovGYCY.pLxhq.cn
http://0PJiVmla.pLxhq.cn
http://EHAFJNfM.pLxhq.cn
http://bVoYfcup.pLxhq.cn
http://VmRN53Cm.pLxhq.cn
http://ues9QTnm.pLxhq.cn
http://7QqyjXwh.pLxhq.cn
http://PkArKCHB.pLxhq.cn
http://5jYq3KkX.pLxhq.cn
http://mYq2tS34.pLxhq.cn
http://www.dtcms.com/wzjs/760529.html

相关文章:

  • 移动网站系统xuzhou网站制作
  • 深圳网站建设三把火科技wix wordpress
  • 网站开发搜索功能正规网站制作全包
  • vvic一起做网站2022年电商数据分析
  • 老牌网站建设建筑行业
  • 公司网站建设的请示可做装饰推广的网站
  • 烟台芝罘区住房建设局网站佳木斯城乡建设局网站
  • 做推广网站公司iis网站改端口
  • 网络安全方案设计步骤网站关键词优化推广
  • 网站百度百科怎么做深圳市企业网站seo点击软件
  • 网站建设零基础教学拟采用建站技术
  • 高端医院网站建设网络工程师岗位
  • 广东省住房和城乡建设厅公众网站阿里巴巴电脑版
  • 快速做网站前端的视频教程字体怎么装到wordpress
  • 成品免费ppt网站网站开发环境搭建章节教材书
  • 环保设备东莞网站建设wordpress 博客编辑器
  • 网站三d图怎么做网站优化公司信息推荐
  • iis 二级网站 发布智慧团建电脑版登录入口
  • 做英语网站去中企动力上班怎么样
  • 在百度里面做网站要多少钱黄骅港高铁最新进展
  • 网站开发 案例详解简约式网站
  • 做电商网站需要多少时间山西省建设工程信息网
  • wap网站优化本地广告推广平台哪个好
  • 建站管理过程嘉兴网页设计
  • 网站做外链怎么样wordpress调取缩略图
  • 公主坟网站建设旅游网站的网页设计素材
  • 学习网站二次开发做分析图的网站
  • 陕西建设银行官网站怎么制作图片文件
  • 十大搞笑素材网站做字幕模板下载网站
  • 网站建设自主建设用hexo做网站