全局组件注册语法
1.组件注册语法
| Vue.component(组件名称, { |
| data: 组件数据, |
| template: 组件模板内容 |
| }) |
| |
| Vue.component('button-counter', { |
| data: function () { |
| return { |
| count: 0 |
| } |
| }, |
| template: '<button v-on:click="count++">点击了{{ count }}次.</button>' |
| }) |
2.组件用法
| <div id="app"> |
| <button-counter></button-counter> |
| </div> |
例
| <!DOCTYPE html> |
| <html lang="en"> |
| <head> |
| <meta charset="UTF-8" /> |
| <title>Document</title> |
| </head> |
| <body> |
| <div id="app"> |
| <button-counter></button-counter> |
| <button-counter></button-counter> |
| <button-counter></button-counter> |
| </div> |
| <script type="text/javascript" src="js/vue.js"></script> |
| <script type="text/javascript"> |
| |
| Vue.component('button-counter', { |
| data: function () { |
| return { |
| count: 0, |
| } |
| }, |
| template: '<button @click="handle">点击了{{count}}次</button>', |
| methods: { |
| handle: function () { |
| this.count += 2 |
| }, |
| }, |
| }) |
| var vm = new Vue({ |
| el: '#app', |
| data: {}, |
| }) |
| </script> |
| </body> |
| </html> |
组件注册注意事项
1.data必须是一个函数
2.组件模板内容必须是单个根元素
3.组件模板内容可以是模板字符串
模板字符串需要浏览器提供支持(ES6语法)
| <!DOCTYPE html> |
| <html lang="en"> |
| <head> |
| <meta charset="UTF-8"> |
| <title>Document</title> |
| </head> |
| <body> |
| <div id="app"> |
| <button-counter></button-counter> |
| <button-counter></button-counter> |
| <button-counter></button-counter> |
| </div> |
| <script type="text/javascript" src="js/vue.js"></script> |
| <script type="text/javascript"> |
| Vue.component('button-counter', { |
| data: function(){ |
| return { |
| count: 0 |
| } |
| }, |
| template: ` |
| <div> |
| <button @click="handle">点击了{{count}}次</button> |
| <button>测试123</button> |
| </div> |
| `, |
| methods: { |
| handle: function(){ |
| this.count += 2; |
| } |
| } |
| }) |
| var vm = new Vue({ |
| el: '#app', |
| data: {} |
| }); |
| </script> |
| </body> |
| </html> |
4.组件命名方式
- 短横线方式
Vue.component('my-component', { /* ... */ })
- 驼峰方式
Vue.component('MyComponent', { /* ... */ })
| <!DOCTYPE html> |
| <html lang="en"> |
| <head> |
| <meta charset="UTF-8"> |
| <title>Document</title> |
| </head> |
| <body> |
| <div id="app"> |
| <button-counter></button-counter> |
| <hello-world></hello-world> |
| </div> |
| <script type="text/javascript" src="js/vue.js"></script> |
| <script type="text/javascript"> |
| |
| |
| |
| |
| |
| Vue.component('HelloWorld', { |
| data: function(){ |
| return { |
| msg: 'HelloWorld' |
| } |
| }, |
| template: '<div>{{msg}}</div>' |
| }); |
| Vue.component('button-counter', { |
| data: function(){ |
| return { |
| count: 0 |
| } |
| }, |
| template: ` |
| <div> |
| <button @click="handle">点击了{{count}}次</button> |
| <button>测试123</button> |
| <HelloWorld></HelloWorld> |
| </div> |
| `, |
| methods: { |
| handle: function(){ |
| this.count += 2; |
| } |
| } |
| }) |
| var vm = new Vue({ |
| el: '#app', |
| data: {} |
| }); |
| </script> |
| </body> |
| </html> |
局部组件注册
| var ComponentA = { } |
| var ComponentB = { } |
| var ComponentC = { } |
| new Vue({ |
| el: '#app' |
| components: { |
| 'component-a': ComponentA, |
| 'component-b': ComponentB, |
| 'component-c': ComponentC, |
| } |
| }) |
| <!DOCTYPE html> |
| <html lang="en"> |
| <head> |
| <meta charset="UTF-8"> |
| <title>Document</title> |
| </head> |
| <body> |
| <div id="app"> |
| <hello-world></hello-world> |
| <hello-tom></hello-tom> |
| <hello-jerry></hello-jerry> |
| <test-com></test-com> |
| </div> |
| <script type="text/javascript" src="js/vue.js"></script> |
| <script type="text/javascript"> |
| |
| |
| |
| |
| Vue.component('test-com',{ |
| template: '<div>Test<hello-world></hello-world></div>' |
| }); |
| var HelloWorld = { |
| data: function(){ |
| return { |
| msg: 'HelloWorld' |
| } |
| }, |
| template: '<div>{{msg}}</div>' |
| }; |
| var HelloTom = { |
| data: function(){ |
| return { |
| msg: 'HelloTom' |
| } |
| }, |
| template: '<div>{{msg}}</div>' |
| }; |
| var HelloJerry = { |
| data: function(){ |
| return { |
| msg: 'HelloJerry' |
| } |
| }, |
| template: '<div>{{msg}}</div>' |
| }; |
| var vm = new Vue({ |
| el: '#app', |
| data: {}, |
| components: { |
| 'hello-world': HelloWorld, |
| 'hello-tom': HelloTom, |
| 'hello-jerry': HelloJerry |
| } |
| }); |
| </script> |
| </body> |
| </html> |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)