vue3 script-setup 语法糖 父子组件通信 使用defineEmit,defineProps,defineExpose (useContext 弃用)

简介: vue3 script-setup 语法糖 父子组件通信 使用defineEmit,defineProps,defineExpose (useContext 弃用)

官方地址

https://github.com/vuejs/rfcs/blob/script-setup-2/active-rfcs/0000-script-setup.md#closed-by-default

新的写法

相比之下写法变得更加简化,下面具体看是否真香

子组件

![在这里插入图片描述](https://ucc.alicdn.com/images/user-upload-01/20210623152824701.png

组件传值

父组件

下面上源码

//子组件
<template>
    <div>
        <div id="editorbox"></div>
        <el-button @click="getHtml">获取文本内容</el-button>
    </div>
</template>
<script setup lang='ts' >
import { ref, onMounted, defineEmits, defineProps, defineExpose } from "vue";
import E from "wangeditor";
const editorRef = ref()
const props = defineProps({
    data: String
})
console.log(props);
const emit = defineEmits(['getHtml'])
const getHtml = () => {
    const texthtml = editorRef.value.txt.html()
    emit("getHtml", texthtml)
}
const setHtml = (val: string) => {
    editorRef.value.txt.html(val) // 重新设置编辑器内
}
const clearnHtml = () => {
    editorRef.value.txt.clear()
}
onMounted(() => {
    const editor = new E('#editorbox')
    editor.config.placeholder = '请输入内容';
    editor.config.menus = [
        // 菜单配置
        "head", // 标题
        "bold", // 粗体
        "fontSize", // 字号
        "fontName", // 字体
        "italic", // 斜体
        "underline", // 下划线
        "strikeThrough", // 删除线
        "foreColor", // 文字颜色
        "backColor", // 背景颜色
        "link", // 插入链接
        "list", // 列表
        "justify", // 对齐方式
        "quote", // 引用
        "emoticon", // 表情
        "image", // 插入图片
        "table", // 表格
        "code", // 插入代码
        "undo", // 撤销
        "redo", // 重复
        "video",//视频
    ]
    editor.config.height = props.data;
    editor.config.zIndex = 100;
    editor.config.showLinkImg = false; // 隐藏“网络图片”tab
    editor.config.showLinkVideo = false//使用本地上传视频
    editor.config.showFullScreen = true //显示全屏按钮
    editor.config.uploadImgShowBase64 = false //用 base64 保存图片
    // editor.config.uploadImgShowBase64 = false; // 使用 base64 保存图片
    // editor.config.pasteText = true; // 只粘贴纯文本
    // editor.config.uploadImgMaxSize = 3 * 1024 * 1024; //限制图片大小
    // editor.config.uploadImgServer = "wdx"; //上传图片地址
    // editor.config.uploadImgHeaders = {
    //     Authorization: 'wdx' // 设置请求头
    // };
    // editor.config.uploadFileName = "file"; //设置字段
    // editor.config.uploadImgMaxLength = 10; // 限制一次最多上传 5 张图片
    // editor.config.uploadImgTimeout = 3 * 60 * 1000; // 设置超时时间
    // editor.config.uploadImgHooks = {
    //     fail: (xhr, editor, result) => {
    //         // 插入图片失败回调
    //     },
    //     success: (xhr, editor, result) => {
    //         // 图片上传成功回调
    //     },
    //     customInsert: (insertImg, result, editor) => {
    //         // 图片上传成功,插入图片的回调
    //         //result为上传图片成功的时候返回的数据,这里我打印了一下发现后台返回的是data:[{url:"路径的形式"},...] 
    //         let url = result.data.url;
    //         insertImg(url);
    //     },
    // }
    editorRef.value = editor
    editor.create();
})
// console.dir(expose);
defineExpose({
    setHtml, getHtml, clearnHtml
})
</script>
<style scoped lang="scss">
</style>

父组件

<template>
    <div>
        {{ boxHeight.data }}
        <EditorChild ref="box" :data="boxHeight.data" @getHtml="getHtml" />
    </div>
</template>
<script setup lang='ts'>
import { ref, onMounted, reactive } from "vue";
import EditorChild from '../components/editor-child.vue'
const getHtml = (e: any) => {
    console.log('获取内容==>', e);
}
const boxHeight = reactive({
    data: 400
})
const box = ref(null)
onMounted(() => {
    console.log(box.value);
    let p = '<p style="color:red">我是红色字体</p>'
    box.value.setHtml(p)
})
</script>
<style scoped lang="scss">
</style>

默认情况下

css变量


相关文章
|
27天前
|
JavaScript 前端开发 安全
Vue 3
Vue 3以组合式API、Proxy响应式系统和全面TypeScript支持,重构前端开发范式。性能优化与生态协同并进,兼顾易用性与工程化,引领Web开发迈向高效、可维护的新纪元。(238字)
458 139
|
21天前
|
缓存 JavaScript 算法
Vue 3性能优化
Vue 3 通过 Proxy 和编译优化提升性能,但仍需遵循最佳实践。合理使用 v-if、key、computed,避免深度监听,利用懒加载与虚拟列表,结合打包优化,方可充分发挥其性能优势。(239字)
173 1
|
2月前
|
开发工具 iOS开发 MacOS
基于Vite7.1+Vue3+Pinia3+ArcoDesign网页版webos后台模板
最新版研发vite7+vue3.5+pinia3+arco-design仿macos/windows风格网页版OS系统Vite-Vue3-WebOS。
327 11
|
1月前
|
JavaScript 安全
vue3使用ts传参教程
Vue 3结合TypeScript实现组件传参,提升类型安全与开发效率。涵盖Props、Emits、v-model双向绑定及useAttrs透传属性,建议明确声明类型,保障代码质量。
214 0
|
3月前
|
缓存 前端开发 大数据
虚拟列表在Vue3中的具体应用场景有哪些?
虚拟列表在 Vue3 中通过仅渲染可视区域内容,显著提升大数据列表性能,适用于 ERP 表格、聊天界面、社交媒体、阅读器、日历及树形结构等场景,结合 `vue-virtual-scroller` 等工具可实现高效滚动与交互体验。
395 1
|
3月前
|
缓存 JavaScript UED
除了循环引用,Vue3还有哪些常见的性能优化技巧?
除了循环引用,Vue3还有哪些常见的性能优化技巧?
218 0
|
4月前
|
JavaScript
vue3循环引用自已实现
当渲染大量数据列表时,使用虚拟列表只渲染可视区域的内容,显著减少 DOM 节点数量。
127 0
|
2月前
|
JavaScript
Vue中如何实现兄弟组件之间的通信
在Vue中,兄弟组件可通过父组件中转、事件总线、Vuex/Pinia或provide/inject实现通信。小型项目推荐父组件中转或事件总线,大型项目建议使用Pinia等状态管理工具,确保数据流清晰可控,避免内存泄漏。
278 2
|
23天前
|
缓存 JavaScript
vue中的keep-alive问题(2)
vue中的keep-alive问题(2)
247 137
|
5月前
|
人工智能 JavaScript 算法
Vue 中 key 属性的深入解析:改变 key 导致组件销毁与重建
Vue 中 key 属性的深入解析:改变 key 导致组件销毁与重建
702 0