Vue3 <script setup> 单文件组件 组合式 API 相关语法

1.vue3使用vuex

javascript 复制代码
<script setup>
import {ref} from "vue"
import {useStore} from "vuex"

//获取store
const store=useStore();
const count =ref(0);
//获取store状态
const type =store.state.type
//给count赋值
count.value=1;

</script>

2.vue3 用router 和 route

javascript 复制代码
<script setup>
import {useRoute,useRouter} from "vue-router"
const route =useRoute();
const router=useRouter();
//获取参数
const type=route.params.type;
//跳转到新的浏览器窗口
function goNewtab(){
const {href} = router.resolve({
	path:'/basic_wx/form'})
	window.open(href, "_blank")
}
</script>

3.vue3 使用 ref

javascript 复制代码
<script setup>
import {ref} from "vue"
const count =ref(0);
//给count赋值
count.value=1;

</script>

4.父子组件通讯

javascript 复制代码
//子组件
<script setup>
import {ref} from "vue"
const count =ref(0);
//引入父级方法
const emits=defineEmits(['getTable'])
//属性props 子级使用父级参
defineProps({
reqParams:Object
});
//显示的指定要暴露出去的属性
defineExpose({
getData,
count
})
//调用父级方法
emits("getTable")
function childMethod(){

}
</script>

//父组件
<script setup>
import {ref} from "vue"
const childDom=ref(null)
import childCom from './childCom.vue'
const reqParams=ref({})
function getTable(){
//方法代码
}
//调用子组件方法
childDom.value.childMethod()
childDom
</script>
<template>
<childCom ref="childDom" :reqParams='reqParams' @getTable="getTable"></childCom>
</template>
相关推荐
小兵张健4 小时前
价值1000的 AI 工作流:Codex 通用前端协作模式
前端·aigc·ai编程
sunny_4 小时前
面试踩大坑!同一段 Node.js 代码,CJS 和 ESM 的执行顺序居然是反的?!99% 的人都答错了
前端·面试·node.js
拉不动的猪5 小时前
移动端调试工具VConsole初始化时的加载阻塞问题
前端·javascript·微信小程序
ayqy贾杰6 小时前
Agent First Engineering
前端·vue.js·面试
IT_陈寒6 小时前
SpringBoot实战:5个让你的API性能翻倍的隐藏技巧
前端·人工智能·后端
iceiceiceice7 小时前
iOS PDF阅读器段评实现:如何从 PDFSelection 精准还原一个自然段
前端·人工智能·ios
大金乄7 小时前
封装一个vue2的elementUI 表格组件(包含表格编辑以及多级表头)
前端·javascript
葡萄城技术团队8 小时前
【性能优化篇】面对万行数据也不卡顿?揭秘协同服务器的“片段机制 (Fragments)”
前端
程序员阿峰8 小时前
2026前端必备:TensorFlow.js,浏览器里的AI引擎,不写Python也能玩转智能
前端
Jans8 小时前
Shipfe — Rust 写的前端静态部署工具:一条命令上线 + 零停机 + 可回滚 + 自动清理
前端