Vue 組件注冊全解析
全局組件注冊語法
components中的兩個參數(shù)組件名稱和組件內(nèi)容
Vue.component(組件名稱, {
data: 組件數(shù)據(jù),
template:組件模板內(nèi)容
})
全局組件注冊應(yīng)用
組件創(chuàng)建:
Vue.component('button-counter', {
data: function(){
return {
count: 0
}
},
template: '<button @click="handle">點擊了{{count}}次</button>',
methods: {
handle: function(){
this.count ++;
}
}
})
var vm = new Vue({
el: '#app',
data: {
}
});
如何在頁面中運用,直接在頁面中應(yīng)用組件名稱
<div id="app"> <button-counter></button-counter> </div>
這個組件是可以重用的,直接在頁面中多次使用,切數(shù)據(jù)相互獨立,互不干擾
組件注冊注意事項
1.data必須是一個函數(shù)
- 分析函數(shù)與普通對象的對比
2.組件模板內(nèi)容必須是單個根元素
- 分析演示實際的效果
3.組件模板內(nèi)容可以是模板字符串
- 模板字符串需要瀏覽器提供支持(ES6語法)
4.組件命名方式
- 短橫線方式
Vue.component('my-component',{/*...*/})
駝峰方式
Vue.component('MyComponent',{/*...*/})
如果使用駝峰式命名組件,那么在使用組件的時候,只能在字符串模板中用駝峰的方式使用組件,但是在普通的標簽?zāi)0逯校仨毷褂枚虣M線的方式使用組件
局部組件
局部組件只能在注冊它的父組件中使用
局部組件注冊語法
var ComponentA = {/*...*/}
var ComponentB = {/*...*/}
var ComponentC = {/*...*/}
new Vue({
el : '#app',
components: {
'component-a' : ComponentA,
'component-b' : ComponentB,
'component-c' : ComponentC
}
})
組件的創(chuàng)建
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
}
});
頁面中的應(yīng)用
<div id="app"> <hello-world></hello-world> <hello-tom></hello-tom> <hello-jerry></hello-jerry> <test-com></test-com> </div>
以上就是Vue 組件注冊全解析的詳細內(nèi)容,更多關(guān)于Vue 組件注冊的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue如何移動到指定位置(scrollIntoView)親測避坑
這篇文章主要介紹了vue如何移動到指定位置(scrollIntoView)親測避坑,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05
Vue 中 reactive創(chuàng)建對象類型響應(yīng)式數(shù)據(jù)的方法
在 Vue 的開發(fā)世界里,響應(yīng)式數(shù)據(jù)是構(gòu)建交互性良好應(yīng)用的基礎(chǔ),之前我們了解了ref用于定義基本類型的數(shù)據(jù),今天就來深入探討一下如何使用reactive定義對象類型的響應(yīng)式數(shù)據(jù),感興趣的朋友一起看看吧2025-02-02
vue3整合SpringSecurity加JWT實現(xiàn)權(quán)限校驗
本文主要介紹了vue3整合SpringSecurity加JWT實現(xiàn)權(quán)限校驗,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2025-04-04
vue改變循環(huán)遍歷后的數(shù)據(jù)實例
今天小編就為大家分享一篇vue改變循環(huán)遍歷后的數(shù)據(jù)實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11

