当前位置: 首页 > news >正文

vue3定义组件

在Vue 3中,定义组件有多种方式,包括使用单文件组件(Single File Components, SFC)、使用JavaScript对象定义组件、以及使用组合式API(Composition API)。

1. 单文件组件(SFC)

这是Vue最常见的组件定义方式,使用.vue文件。

示例:MyComponent.vue
<template><div><h1>{{ title }}</h1><button @click="increment">Click me</button><p>Count: {{ count }}</p></div>
</template><script>
export default {name: 'MyComponent',data() {return {title: 'Hello, Vue 3!',count: 0};},methods: {increment() {this.count++;}}
};
</script><style scoped>
h1 {color: blue;
}
</style>
使用组件
<template><div id="app"><MyComponent /></div>
</template><script>
import MyComponent from './components/MyComponent.vue';export default {name: 'App',components: {MyComponent}
};
</script>

2. 使用JavaScript对象定义组件

你可以直接在JavaScript文件中定义组件。

示例:MyComponent.js
export default {name: 'MyComponent',template: `<div><h1>{{ title }}</h1><button @click="increment">Click me</button><p>Count: {{ count }}</p></div>`,data() {return {title: 'Hello, Vue 3!',count: 0};},methods: {increment() {this.count++;}}
};
使用组件
<template><div id="app"><MyComponent /></div>
</template><script>
import MyComponent from './components/MyComponent.js';export default {name: 'App',components: {MyComponent}
};
</script>

3. 使用组合式API(Composition API)

Vue 3引入了组合式API,使得组件逻辑更加灵活和可重用。

示例:MyComponent.vue
<template><div><h1>{{ title }}</h1><button @click="increment">Click me</button><p>Count: {{ count }}</p></div>
</template><script>
import { ref } from 'vue';export default {name: 'MyComponent',setup() {const title = ref('Hello, Vue 3!');const count = ref(0);const increment = () => {count.value++;};return {title,count,increment};}
};
</script><style scoped>
h1 {color: blue;
}
</style>
使用组件
<template><div id="app"><MyComponent /></div>
</template><script>
import MyComponent from './components/MyComponent.vue';export default {name: 'App',components: {MyComponent}
};
</script>

4. 全局注册组件

你也可以全局注册组件,这样就不需要在每个使用的地方单独引入。

示例:main.js
import { createApp } from 'vue';
import App from './App.vue';
import MyComponent from './components/MyComponent.vue';const app = createApp(App);app.component('MyComponent', MyComponent);app.mount('#app');
使用组件
<template><div id="app"><MyComponent /></div>
</template><script>
export default {name: 'App'
};
</script>
http://www.lryc.cn/news/457018.html

相关文章:

  • BOM常见操作方法汇总
  • Python+whisper/vosk实现语音识别
  • 如何在算家云搭建LivePortrait(视频生成)
  • CSS 命名规范及 BEM 在前端开发中的实践
  • SwiftUI 6.0(iOS 18)新增的网格渐变色 MeshGradient 解惑
  • 【计算机网络】详谈TCP协议确认应答机制捎带应答机制超市重传机制连接管理机制流量管理机制滑动窗口拥塞控制延迟应答
  • rk3566开发之rknn npu 部署
  • 项目生产经理需要具备哪些技能和素质
  • Java数据类型常量
  • 如何提高浮点类型计算的精度
  • RabbitMQ简介及安装类
  • 游戏服务器防御策略:防止玩家因DDoS攻击而掉线
  • Django学习笔记二:数据库操作详解
  • Spring Boot 应用开发案例:在线书籍管理系统
  • 阿里140滑块-滑块验证码逆向分析思路学习
  • 使用 Linux 搭建并配置一个 NFS 服务器
  • python发包
  • 农行1面:说说 final,finally,finalize的区别
  • ChatGPT实时语音将于本周向免费用户推出:OpenAI DevDay 2024详细解读
  • 一个月学会Java 第7天 字符串与键盘输入
  • Java后端面试题(day16)
  • Auto-Animate:是一款零配置、即插即用的动画工具,可以为您的 Web 应用添加流畅的过渡效果
  • k8s之ingress-nginx-controller安装
  • 力扣SQL仅数据库(1098~1132)
  • 优惠点餐api接口对接的具体步骤是什么?
  • 【韩顺平Java笔记】第8章:面向对象编程(中级部分)【297-313】
  • 快递批量查询物流追踪只揽收无物流信息的单号
  • 【动态网站资源保存下载】
  • Selenium自动化测试中如何处理数据驱动?
  • 淘宝API接口系列有哪些内容?