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>
相关推荐
C澒6 分钟前
SGW 接入层运维实战:配置查看 + 监控分析 + 日志排查
前端·安全·运维开发
德育处主任Pro33 分钟前
『NAS』在群晖部署一款太空策略游戏-ogame-vue-ts
前端·vue.js·游戏
ziqi5221 小时前
第二十五天笔记
前端·chrome·笔记
GISer_Jing1 小时前
Memory、Rules、Skills、MCP如何重塑AI编程
前端·人工智能·aigc·ai编程
xcs194051 小时前
前端 项目构建问题 \node_modules\loader-runner\lib\loadLoader.js
开发语言·前端·javascript
广然1 小时前
EVE-NG 镜像管理工具 1.1 Web 版本正式发布!
运维·服务器·前端
Data_Journal1 小时前
【无标题】
大数据·服务器·前端·数据库·人工智能
我爱加班、、2 小时前
new Map()+Array.from()整理elementPlus的级联器数据
linux·前端·javascript
Hx_Ma162 小时前
Map集合的5种遍历方式
java·前端·javascript
css趣多多2 小时前
render函数
前端·javascript·vue.js