谁可以告诉我vue3的setup语法糖设置了不起作用是什么原因
急
1条回答 默认 最新
关注 - 这个问题的回答你可以参考下: https://ask.csdn.net/questions/7608506
- 这篇博客也不错, 你可以看下Vue3中自定义事件实现子组件向父组件传递数据
- 除此之外, 这篇博客: vue3 setup语法糖 详细使用说明中的 那我就来介绍一下 setup 语法糖的用法: 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
- 挂在组件
<template> <Index /> </template> <script setup> // 不需要挂载注册 import Index from './index.vue'; </script>
- 使用 refs 的一系列操作
// 父组件 Father.vue <template> <Index ref="IndexRefs" /> </template> <script setup> import Index from './index.vue'; import { onMounted , ref } from 'vue'; const IndexRefs = ref(null) onMounted (()=>{ IndexRefs.value.num +1 // 1 IndexRefs.value.click() // 2 }) </script> // 子组件 Index.vue <template> num:{{ num }} </template> <script setup> import { ref } from 'vue'; const num = ref(0) const click = () =>{ num .value +2 } // 父级想操作的数据 或者 方法导出去 defineExpose({ num click }); </script>
- 组件之间 相互 传参
// 父传子 // 父组件 Father.vue <template> <Index :num="num" /> </template> <script setup> import Index from './index.vue'; import { ref } from 'vue'; const num = ref(10) </script> // 子组件 Index.vue <template> {{ num }} </template> <script setup> // 接收值 const { num } = defineProps({ num : Number, }); console.log('num' , num) </script> // 子传父 // 父组件 Father.vue <template> <Index @porpsClick="btn" /> </template> <script setup> import Index from './index.vue'; const btn = (_porps) => { console.log('_porps' , _porps) // 123 } </script> // 子组件 Index.vue <template> <button @click="btn">点我传参</button> </template> <script setup> const emit = defineEmits(['porpsClick']); const btn = () =>{ emit( 'porpsClick' , 123 ) } </script>
- useSlots 和 useAttrs 使用
<script setup> import { useAttrs, useSlots } from 'vue'; const attrs = useAttrs(); const slots = useSlots(); </script>
此文章是为了记录学习,
有什么没提到的可以交流,互相学习- 您还可以看一下 张益珲老师的循序渐进Vue3.x开发入门课程中的 前端技术演进小节, 巩固相关知识点
解决 无用评论 打赏 举报
悬赏问题
- ¥15 三极管电路求解,已知电阻电压和三级关放大倍数
- ¥15 ADS时域 连续相位观察方法
- ¥15 Opencv配置出错
- ¥15 模电中二极管,三极管和电容的应用
- ¥15 关于模型导入UNITY的.FBX: Check external application preferences.警告。
- ¥15 气象网格数据与卫星轨道数据如何匹配
- ¥100 java ee ssm项目 悬赏,感兴趣直接联系我
- ¥15 微软账户问题不小心注销了好像
- ¥15 x264库中预测模式字IPM、运动向量差MVD、量化后的DCT系数的位置
- ¥15 curl 命令调用正常,程序调用报 java.net.ConnectException: connection refused