程序员scholar 程序员scholar
首页
  • Java 基础

    • JavaSE
    • JavaIO
    • JavaAPI速查
  • Java 高级

    • JUC
    • JVM
    • Java新特性
    • 设计模式
  • Web 开发

    • Servlet
    • Java网络编程
  • Web 标准

    • HTML
    • CSS
    • JavaScript
  • 前端框架

    • Vue2
    • Vue3
    • Vue3 + TS
    • 微信小程序
    • uni-app
  • 工具与库

    • jQuery
    • Ajax
    • Axios
    • Webpack
    • Vuex
    • WebSocket
    • 第三方登录
  • 后端与语言扩展

    • ES6
    • Typescript
    • node.js
  • Element-UI
  • Apache ECharts
  • 数据结构
  • HTTP协议
  • HTTPS协议
  • 计算机网络
  • Linux常用命令
  • Windows常用命令
  • SQL数据库

    • MySQL
    • MySQL速查
  • NoSQL数据库

    • Redis
    • ElasticSearch
  • 数据库

    • MyBatis
    • MyBatis-Plus
  • 消息中间件

    • RabbitMQ
  • 服务器

    • Nginx
  • Spring框架

    • Spring6
    • SpringMVC
    • SpringBoot
    • SpringSecurity
  • SpringCould微服务

    • SpringCloud基础
    • 微服务之DDD架构思想
  • 日常必备

    • 开发常用工具包
    • Hutoll工具包
    • IDEA常用配置
    • 开发笔记
    • 日常记录
    • 项目部署
    • 网站导航
    • 产品学习
    • 英语学习
  • 代码管理

    • Maven
    • Git教程
    • Git小乌龟教程
  • 运维工具

    • Docker
    • Jenkins
    • Kubernetes
  • 算法笔记

    • 算法思想
    • 刷题笔记
  • 面试问题常见

    • 十大经典排序算法
    • 面试常见问题集锦
关于
GitHub (opens new window)
首页
  • Java 基础

    • JavaSE
    • JavaIO
    • JavaAPI速查
  • Java 高级

    • JUC
    • JVM
    • Java新特性
    • 设计模式
  • Web 开发

    • Servlet
    • Java网络编程
  • Web 标准

    • HTML
    • CSS
    • JavaScript
  • 前端框架

    • Vue2
    • Vue3
    • Vue3 + TS
    • 微信小程序
    • uni-app
  • 工具与库

    • jQuery
    • Ajax
    • Axios
    • Webpack
    • Vuex
    • WebSocket
    • 第三方登录
  • 后端与语言扩展

    • ES6
    • Typescript
    • node.js
  • Element-UI
  • Apache ECharts
  • 数据结构
  • HTTP协议
  • HTTPS协议
  • 计算机网络
  • Linux常用命令
  • Windows常用命令
  • SQL数据库

    • MySQL
    • MySQL速查
  • NoSQL数据库

    • Redis
    • ElasticSearch
  • 数据库

    • MyBatis
    • MyBatis-Plus
  • 消息中间件

    • RabbitMQ
  • 服务器

    • Nginx
  • Spring框架

    • Spring6
    • SpringMVC
    • SpringBoot
    • SpringSecurity
  • SpringCould微服务

    • SpringCloud基础
    • 微服务之DDD架构思想
  • 日常必备

    • 开发常用工具包
    • Hutoll工具包
    • IDEA常用配置
    • 开发笔记
    • 日常记录
    • 项目部署
    • 网站导航
    • 产品学习
    • 英语学习
  • 代码管理

    • Maven
    • Git教程
    • Git小乌龟教程
  • 运维工具

    • Docker
    • Jenkins
    • Kubernetes
  • 算法笔记

    • 算法思想
    • 刷题笔记
  • 面试问题常见

    • 十大经典排序算法
    • 面试常见问题集锦
关于
GitHub (opens new window)
npm

(进入注册为作者充电)

  • Vue2

  • Vue3

    • 初识Vue3
    • 组合式API
    • Vue3中的data数据
    • vue3 的 script标签
    • setup函数中执行顺序
    • toRefs() 与 toRef()
    • computed()
    • Vue3弱化this
    • watch() 和 watchEffec()
    • define函数用法
      • 1. `props` 在 Vue2 中 vs `defineProps` 在 Vue3 中
        • Vue2:`props`
        • Vue3:`defineProps`
      • 2. `emit` 在 Vue2 中 vs `defineEmits` 在 Vue3 中
        • Vue2:`emit`
        • Vue3:`defineEmits`
      • 3. `expose` 在 Vue2 中 vs `defineExpose` 在 Vue3 中
        • Vue2:公开方法或属性
        • Vue3:`defineExpose`
      • 4. 异步组件:Vue2 的 `Vue.component` vs Vue3 的 `defineAsyncComponent`
        • Vue2:异步组件
        • Vue3:`defineAsyncComponent`
      • 5. Web 组件:Vue2 中使用 `Vue.customElement` vs Vue3 的 `defineCustomElement`
        • Vue2:使用 `vue-custom-element` 插件
        • Vue3:`defineCustomElement`
    • defineExpose() 和 ref 属性
    • vue.config.js
    • 生命周期
    • Vue3全局API调用
    • 自定义 Hook
    • 传递数据(props)
    • 路由
    • pinia
    • 组件通信
    • getCurrentInstance() 和 nextTick()
    • Vue3 新组件
  • vue3 + TS 项目集成

  • Vue全家桶
  • Vue3
scholar
2024-09-01
目录

define函数用法

# define函数用法

# 1. props 在 Vue2 中 vs defineProps 在 Vue3 中

# Vue2:props

在Vue2中,props用于从父组件传递数据到子组件。你可以在props选项中定义属性,并在模板中直接使用。

<template>
  <div>
    <h1>{{ title }}</h1>
  </div>
</template>

<script>
export default {
  // 定义接收的 props
  props: {
    title: {
      type: String,
      required: true
    }
  }
};
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# Vue3:defineProps

在Vue3中,defineProps在<script setup>中使用,直接定义和访问组件的props。

<template>
  <div>
    <h1>{{ title }}</h1>
  </div>
</template>

<script setup>
// 通过 defineProps 定义和获取 props
const props = defineProps({
  title: {
    type: String,
    required: true
  }
});
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

解释:

  • 在Vue2中,props是在组件选项中定义的。
  • 在Vue3中,defineProps在<script setup>中使用,提供了一种更简洁的方式来定义和使用props。

# 2. emit 在 Vue2 中 vs defineEmits 在 Vue3 中

# Vue2:emit

在Vue2中,$emit用于在子组件中触发事件,并可以通过事件名称传递数据。

<template>
  <button @click="handleClick">Click me</button>
</template>

<script>
export default {
  methods: {
    handleClick() {
      // 触发事件,传递数据
      this.$emit('myEvent', 'Hello from child');
    }
  }
};
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# Vue3:defineEmits

在Vue3中,defineEmits用于声明可以触发的事件,并通过emit函数触发这些事件。

<template>
  <button @click="handleClick">Click me</button>
</template>

<script setup>
// 定义组件的事件
const emit = defineEmits(['myEvent']);

function handleClick() {
  // 触发事件,传递数据
  emit('myEvent', 'Hello from child');
}
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13

解释:

  • 在Vue2中,使用this.$emit触发事件。
  • 在Vue3中,使用defineEmits声明事件,并使用emit函数触发这些事件。

# 3. expose 在 Vue2 中 vs defineExpose 在 Vue3 中

# Vue2:公开方法或属性

在Vue2中,如果你想让父组件访问子组件的方法或属性,你可以通过ref在父组件中直接访问子组件实例,并调用其方法。

子组件:

<template>
  <div>{{ count }}</div>
</template>

<script>
export default {
  data() {
    return {
      count: 0
    };
  },
  methods: {
    increment() {
      this.count++;
    }
  }
};
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

父组件:

<template>
  <ChildComponent ref="childComp" />
  <button @click="incrementChild">Increment Child</button>
</template>

<script>
import ChildComponent from './ChildComponent.vue';

export default {
  components: {
    ChildComponent
  },
  methods: {
    incrementChild() {
      this.$refs.childComp.increment();
    }
  }
};
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

# Vue3:defineExpose

在Vue3中,通过defineExpose显式地暴露子组件内部的状态或方法,供父组件使用。

子组件:

<template>
  <div>{{ count }}</div>
</template>

<script setup>
import { ref, defineExpose } from 'vue';

const count = ref(0);

function increment() {
  count.value++;
}

// 显式暴露 count 和 increment 方法
defineExpose({
  count,
  increment
});
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

父组件:

<template>
  <ChildComponent ref="childComp" />
  <button @click="incrementChild">Increment Child</button>
</template>

<script setup>
import { ref } from 'vue';
import ChildComponent from './ChildComponent.vue';

// 访问子组件的暴露方法
const childComp = ref(null);

function incrementChild() {
  childComp.value.increment();
}
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

解释:

  • 在Vue2中,通过$refs可以访问子组件实例,并调用其方法。
  • 在Vue3中,通过defineExpose显式暴露方法或属性,使父组件可以通过ref访问和调用。

# 4. 异步组件:Vue2 的 Vue.component vs Vue3 的 defineAsyncComponent

# Vue2:异步组件

在Vue2中,可以通过Vue.component注册异步组件,这些组件会在需要时加载。

Vue.component('AsyncComponent', function (resolve) {
  // 通过 resolve 函数异步加载组件
  require(['./components/HeavyComponent.vue'], resolve);
});
1
2
3
4

或者通过import语法:

Vue.component('AsyncComponent', () => import('./components/HeavyComponent.vue'));
1

# Vue3:defineAsyncComponent

在Vue3中,使用defineAsyncComponent来定义异步组件,组件会在首次使用时加载。

import { defineAsyncComponent } from 'vue';

const AsyncComponent = defineAsyncComponent(() =>
  import('./components/HeavyComponent.vue')
);

export default {
  components: {
    AsyncComponent
  }
};
1
2
3
4
5
6
7
8
9
10
11

解释:

  • 在Vue2中,异步组件通过Vue.component进行注册,并使用回调或import进行异步加载。
  • 在Vue3中,使用defineAsyncComponent可以更简洁地定义异步组件,并支持更强大的功能(如超时处理、加载状态等)。

# 5. Web 组件:Vue2 中使用 Vue.customElement vs Vue3 的 defineCustomElement

# Vue2:使用 vue-custom-element 插件

在Vue2中,如果想将Vue组件作为原生Web组件(Custom Element)使用,通常会借助vue-custom-element插件。

import Vue from 'vue';
import vueCustomElement from 'vue-custom-element';
import MyComponent from './MyComponent.vue';

Vue.use(vueCustomElement);

// 将 Vue 组件注册为 Custom Element
Vue.customElement('my-custom-element', MyComponent);
1
2
3
4
5
6
7
8

# Vue3:defineCustomElement

在Vue3中,defineCustomElement原生支持将Vue组件定义为Web组件。

import { defineCustomElement } from 'vue';
import MyComponent from './MyComponent.vue';

// 将 Vue 组件注册为 Custom Element
const MyCustomElement = defineCustomElement(MyComponent);

customElements.define('my-custom-element', MyCustomElement);
1
2
3
4
5
6
7

解释:

  • 在Vue2中,通常需要使用第三方插件(如vue-custom-element)来将Vue组件转换为Web组件。
  • 在Vue3中,defineCustomElement函数提供了内置支持,将Vue组件注册为Web组件,无需外部插件。
编辑此页 (opens new window)
上次更新: 2025/01/30, 23:55:43
watch() 和 watchEffec()
defineExpose() 和 ref 属性

← watch() 和 watchEffec() defineExpose() 和 ref 属性→

Theme by Vdoing | Copyright © 2019-2025 程序员scholar
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式