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>
相关推荐
PineappleCoder5 小时前
性能数据别再瞎轮询了!PerformanceObserver 异步捕获 LCP/CLS,不卡主线程
前端·性能优化
PineappleCoder5 小时前
告别字体闪烁 / 首屏卡顿!preload 让关键资源 “高优先级” 提前到
前端·性能优化
m0_471199635 小时前
【vue】通俗详解package-lock文件的作用
前端·javascript·vue.js
GIS之路6 小时前
GDAL 读取KML数据
前端
今天不要写bug6 小时前
vue项目基于vue-cropper实现图片裁剪与图片压缩
前端·javascript·vue.js·typescript
用户47949283569156 小时前
记住这张时间线图,你再也不会乱用 useEffect / useLayoutEffect
前端·react.js
汝生淮南吾在北6 小时前
SpringBoot+Vue养老院管理系统
vue.js·spring boot·后端·毕业设计·毕设
咬人喵喵7 小时前
14 类圣诞核心 SVG 交互方案拆解(附案例 + 资源)
开发语言·前端·javascript
问君能有几多愁~7 小时前
C++ 日志实现
java·前端·c++