日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > vue >内容正文

vue

017_Vue组件

發(fā)布時間:2025/5/22 vue 49 豆豆
生活随笔 收集整理的這篇文章主要介紹了 017_Vue组件 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1. 全局組件

1.1. 全局組件注冊語法

Vue.component(組件名稱, {data: 組件數(shù)據(jù),template: 組件模板內(nèi)容,methods: {} })

1.2. 組件使用, 組件可以重用

<div id="app"><組件名稱></組件名稱><組件名稱></組件名稱><組件名稱></組件名稱> </div>

1.3. 代碼

<!DOCTYPE html> <html><head><meta charset="utf-8" /><title>全局組件</title></head><body><div id="app"><button-counter></button-counter><button-counter></button-counter><button-counter></button-counter></div><script type="text/javascript" src="vue.min.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 += 1;}}});var vm = new Vue({el: "#app",data: {}});</script></body> </html>

1.4. 效果圖

1.5. 組件的注意事項

1.5.1. data必須是一個函數(shù)。

1.5.2. 組件模板內(nèi)容必須是單個根元素。

2. 模板字符串

2.1. 組件模板內(nèi)容可以是模板字符串, 模板字符串需要瀏覽器提供支持(ES6語法), 解決模板內(nèi)容較長問題。

?

2.2. 代碼?

<!DOCTYPE html> <html><head><meta charset="utf-8" /><title>組件模板字符串</title></head><body><div id="app"><button-counter></button-counter></div><script type="text/javascript" src="vue.min.js"></script><script type="text/javascript">Vue.component("button-counter", {data: function() {return {count: 0}},template: `<div><button @click="handlePlus">加加{{count}}</button><button @click="handleMinus">減減{{count}}</button></div>`,methods: {handlePlus: function(){++this.count;},handleMinus: function(){--this.count;}}});var vm = new Vue({el: "#app",data: {}});</script></body> </html>

2.3. 效果圖

3. 組件命名方式

3.1. 短橫線方式

Vue.component('my-component', {})

3.2. 駝峰方式(盡可能不用)

Vue.component('MyComponent', {})

3.3.?如果使用駝峰式命名組件, 那么在使用組件的時候, 只能在字符串模板中用駝峰的方式使用組件, 但是在普通的標簽?zāi)0逯? 必須使用短橫線的方式使用組件。

3.4.?代碼

<!DOCTYPE html> <html><head><meta charset="utf-8" /><title>組件命名</title></head><body><div id="app"><hello-world></hello-world><button-counter></button-counter></div><script type="text/javascript" src="vue.min.js"></script><script type="text/javascript">// 如果使用駝峰式命名組件, 那么在使用組件的時候, 只能在字符串模板中用駝峰的方式使用組件, 但是在普通的標簽?zāi)0逯? 必須使用短橫線的方式使用組件。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><HelloWorld></HelloWorld></div>`,methods: {handle: function(){++this.count;}}});var vm = new Vue({el: "#app",data: {}});</script></body> </html>

3.5.?效果圖

4. 局部組件

4.1. 局部組件使用方式

?

4.2.?代碼?

<!DOCTYPE html> <html><head><meta charset="utf-8" /><title>局部組件</title></head><body><div id="app"><hello-world></hello-world><button-counter></button-counter></div><script type="text/javascript" src="vue.min.js"></script><script type="text/javascript">var helloWorld = {data: function() {return {msg: "HelloWorld"}},template: "<div>{{msg}}</div>"};var buttonCounter = {data: function(){return {count: 0}},template: "<button @click='handle'>點擊了{{count}}次</button>",methods: {handle: function(){this.count += 1;}}}var vm = new Vue({el: "#app",components: {'hello-world': helloWorld,'button-counter': buttonCounter}});</script></body> </html>

4.3.?效果圖

?

總結(jié)

以上是生活随笔為你收集整理的017_Vue组件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。