vue3的个人理解

本文目的是记录使用vue3的时候的一些属性的个人理解。

一、defineExpose
  1. 官方说明
    在这里插入图片描述
  2. 个人理解
    <script setup>组件中,defineExpose可以暴露出组件的属性。
  3. 项目实战
    // dialog组件
    <script setup>
    	import { ref } from 'vue'
    	import { Close } from '@element-plus/icons-vue'
    	// dialog 是否可见
    	const dialogVisible = ref(false)
    	// 配置
    	const config = ref({})
    	// 回调
    	let callBack = null
    	
    	// 组件暴露出的方法和属性
    	defineExpose({
    	  openDialog(_config) {
    	    config.value = _config
    	    config.value.id = _config.id
    	    config.value.width = _config.width || '50%' // 默认宽度50%,允许自定义宽度
    	    callBack = _config.callback
    	    dialogVisible.value = true
    	  },
    	})
    	
    	</script>
    	<template>
    	  <el-dialog
    	    v-model="dialogVisible"
    	    align-center
    	    class="i-dialog"
    	    :show-close="false"
    	    :append-to-body="true"
    	    :width="config.width"
    	  >
    	  </el-dialog>
    	</template>
    
    	// 父页面
    	const dialog = ref()
    	
    	function handleStopApi(id) {
    	  dialog.value.openDialog({
    	    width: '480',
    	    title: '您确认要停用该企业吗?',
    	    message: '停用后该企业无法再次登录系统!',
    	    confirmText: '停用',
    	    callback: () => {
    	      disableOrg(id)
    	    },
    	    id,
    	  })
    	}
    	
    	<templete>
    	    <Dialog ref="dialog" />
    	</templete>
    
二、自定义指令
  1. 官方说明
    在这里插入图片描述
  2. 个人理解
    自定义指令,写好自定义指令后,注册指令,页面中通过v-***调用
  3. 项目实战
// src\directives\throttle.js
import throttle from 'lodash/throttle';

export default function throttleDirective() {
  return {
    fn: null,
    mounted(el, binding) {
      const handler = Array.isArray(value) ? value[0] : value;
      const delay = Array.isArray(value) ? value[1] : 1000; // Default delay to 1000 ms if not provided
      const handleThrottle = throttle(handler, delay);
      el.addEventListener(arg, handleThrottle);
    },
  };
}

// main.js
import throttleDirective from '@/directives/throttle.js'

app.directive('throttle', throttleDirective())

// 使用自定义组件
<template>
  <button v-throttle:click="[handleClick, 2000]">Click Me</button>
  <button v-throttle:click="handleClick">Click Me</button>
</template>

<script>
export default {
  methods: {
    handleClick() {
      console.log('Button clicked!');
    },
  },
};
</script>

defineModel
  1. 官方说明
    在这里插入图片描述
  2. 个人理解
    父子组件双向数据绑定使用defineModel, 比以前的写法更加便捷
	<!-- Child.vue -->
	<script setup>
	const props = defineProps(['modelValue'])
	const emit = defineEmits(['update:modelValue'])
	</script>
	
	<template>
	  <input
	    :value="props.modelValue"
	    @input="emit('update:modelValue', $event.target.value)"
	  />
	</template>

相关推荐

  1. vue一些个人理解

    2024-06-11 18:44:08       44 阅读
  2. 深入理解Vue.js 3Reactive方法

    2024-06-11 18:44:08       63 阅读
  3. VUE3v-text、v-html、:style理解

    2024-06-11 18:44:08       37 阅读
  4. 深入理解Vue 3自定义Hooks

    2024-06-11 18:44:08       151 阅读
  5. Vue 3 Hooks: 深入理解 Composition API 魅力

    2024-06-11 18:44:08       35 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-11 18:44:08       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-11 18:44:08       106 阅读
  3. 在Django里面运行非项目文件

    2024-06-11 18:44:08       87 阅读
  4. Python语言-面向对象

    2024-06-11 18:44:08       96 阅读

热门阅读

  1. C++中的命令模式

    2024-06-11 18:44:08       24 阅读
  2. 结构化表达,了解python的pep

    2024-06-11 18:44:08       32 阅读
  3. 关系模式R(U,F)【数据库-软件设计师】

    2024-06-11 18:44:08       28 阅读
  4. 常用的三种软件架构

    2024-06-11 18:44:08       30 阅读
  5. GMT shp转gmt数据

    2024-06-11 18:44:08       28 阅读
  6. 数据库文件的简单设计

    2024-06-11 18:44:08       23 阅读
  7. 关于AD9781芯片的说明以及FPGA控制实现 I

    2024-06-11 18:44:08       30 阅读
  8. Web前端后端精通:深度解析与技能进阶

    2024-06-11 18:44:08       29 阅读
  9. Ascend ATC相关参数说明和描述

    2024-06-11 18:44:08       30 阅读
  10. Android:UI:Drawable:View/ImageView与Drawable

    2024-06-11 18:44:08       27 阅读
  11. 【Linux】另一种基于rpm安装yum的方式

    2024-06-11 18:44:08       34 阅读
  12. 一五一、Go入门到进阶:并发编程

    2024-06-11 18:44:08       21 阅读
  13. PHP运算符:从基础到高级

    2024-06-11 18:44:08       27 阅读