vue3.0规范学习记录

  1. 组合式函数使用use+name进行命名,例如useMouse;

  2. 自定义指令使用v + name进行命名,例如vFocus;

  3. 在组件使用v-model实现“双向绑定”时,子组件默认通过emits(‘update:modelValue’, params)触发更新;

  4. setup() 钩子是在组件中使用组合式 API 的入口,通常只在以下情况下使用:

    1. 需要在非单文件组件中使用组合式 API 时。
    2. 需要在基于选项式 API 的组件中集成基于组合式 API 的代码时.

    对于结合单文件组件使用的组合式 API,推荐通过 <script setup> 以获得更加简洁及符合人体工程学的语法。

  5. 通过PascalCase(大驼峰)格式引入组件,模板也使用PascalCase(大驼峰);

    import MyInput from '../component/MyInput.vue'
    // 模板
    <MyInput />
    
  6. defineProps 和 defineEmits 都是只能在<script setup> 中使用的编译器宏。他们不需要导入,且会随着 <script setup> 的处理过程一同被编译掉;

  7. 单文件组件<script setup>语法,接收props值:

    • 非TS组件:props接收参数时使用defineProps,如下:
    <script setup>
    import {
          defineProps } from "vue";
    const props = defineProps({
         
      name: {
         
        type: String,
        default: "张三"
      },
      labels: {
         
        type: Array,
        default: () => ["姓名", "年龄"]
      }
    });
    </script>
    
    <template>
      <div>{
         {
          props.name }}</div>
      <div>{
         {
          props.labels }}</div>
    </template>
    
    • TS组件:使用接口 + defineProps声明,如需设置默认值,使用withDefaults编译器宏,如下:
    interface Props {
         
    	msg: string,
    	labels?: string []
    }
    const props = withDefaults(defineProps<Props>(), {
         
    	msg: "特点",
    	labels: () => ["上进", "应变能力不错"]
    })
    
  8. 单文件组件<script setup>语法,声明emit事件:

    1. 非TS:使用defineEmits声明,如下:
    const emits = defineEmits(["input", "change"])
    emits('input')
    
    1. TS:使用接口+defineEmits声明,如下:
    interface Emits {
         
    	(e: 'change', id: number): void,
    	(e: 'update', value: string): void
    }
    const emits = defineEmits<Emits>()
    emits('change', 1)
    
    // 3.3+:另一种更简洁的语法
    const emit = defineEmits<{
         
      change: [id: number] // 具名元组语法
      update: [value: string]
    }>()
    

相关推荐

  1. 【前端学习记录Vue前端规范整理

    2024-01-24 15:38:01       64 阅读
  2. vue3.0规范学习记录

    2024-01-24 15:38:01       53 阅读
  3. 代码学习记录36---动态规划

    2024-01-24 15:38:01       46 阅读
  4. 代码学习记录39---动态规划

    2024-01-24 15:38:01       33 阅读
  5. vue3学习记录

    2024-01-24 15:38:01       54 阅读
  6. Vue3学习记录

    2024-01-24 15:38:01       23 阅读

最近更新

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

    2024-01-24 15:38:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-24 15:38:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-24 15:38:01       82 阅读
  4. Python语言-面向对象

    2024-01-24 15:38:01       91 阅读

热门阅读

  1. FPGA 时钟资源

    2024-01-24 15:38:01       55 阅读
  2. MySQL 随机查询10条数据

    2024-01-24 15:38:01       54 阅读
  3. element-ui 打包流程源码解析——babel 相关

    2024-01-24 15:38:01       64 阅读
  4. Ubuntu 18.04 dns掉配置的问题解决

    2024-01-24 15:38:01       61 阅读
  5. vue3中几种封装让后端传参请求方式

    2024-01-24 15:38:01       50 阅读
  6. 边缘计算:在挑战与机遇的浪潮中破浪前行

    2024-01-24 15:38:01       54 阅读
  7. 边缘计算的挑战和机遇

    2024-01-24 15:38:01       59 阅读
  8. Dart/Flutter工具模块:the_utils

    2024-01-24 15:38:01       63 阅读
  9. 《设计模式的艺术》笔记 - 备忘录模式

    2024-01-24 15:38:01       53 阅读