詳解vue.js全局組件和局部組件
這兩天學(xué)習(xí)了Vue.js 感覺組件這個地方知識點挺多的,而且很重要,所以,今天添加一點小筆記。
首先Vue組件的使用有3個步驟,創(chuàng)建組件構(gòu)造器,注冊組件,使用組件3個方面。
代碼演示如下:
<!DOCTYPE html>
<html>
<body>
<div id="app">
<!-- 3. #app是Vue實例掛載的元素,應(yīng)該在掛載元素范圍內(nèi)使用組件-->
<my-component></my-component>
</div>
</body>
<script src="js/vue.js"></script>
<script>
// 1.創(chuàng)建一個組件構(gòu)造器
var myComponent = Vue.extend({
template: '<div>This is my first component!</div>'
})
// 2.注冊組件,并指定組件的標(biāo)簽,組件的HTML標(biāo)簽為<my-component>
Vue.component('my-component', myComponent)
new Vue({
el: '#app'
});
</script>
</html>
2.理解組件的創(chuàng)建和注冊
我們用以下幾個步驟來理解組件的創(chuàng)建和注冊:
1. Vue.extend()是Vue構(gòu)造器的擴展,調(diào)用Vue.extend()創(chuàng)建的是一個組件構(gòu)造器,而不是一個具體的組件實例。
2. Vue.extend()構(gòu)造器有一個選項對象,選項對象的template屬性用于定義組件要渲染的HTML。
3. 使用Vue.component()注冊組件時,需要提供2個參數(shù),第1個參數(shù)時組件的標(biāo)簽,第2個參數(shù)是組件構(gòu)造器。
4. Vue.component()方法內(nèi)部會調(diào)用組件構(gòu)造器,創(chuàng)建一個組件實例。
5. 組件應(yīng)該掛載到某個Vue實例下,否則它不會生效。
請注意第5點,以下代碼在3個地方使用了<my-component>標(biāo)簽,但只有#app1和#app2下的<my-component>標(biāo)簽才起到作用。
<!DOCTYPE html>
<html>
<body>
<div id="app1">
<my-component></my-component>
</div>
<div id="app2">
<my-component></my-component>
</div>
<!--該組件不會被渲染-->
<my-component></my-component>
</body>
<script src="js/vue.js"></script>
<script>
var myComponent = Vue.extend({
template: '<div>This is a component!</div>'
})
Vue.component('my-component', myComponent)
var app1 = new Vue({
el: '#app1'
});
var app2 = new Vue({
el: '#app2'
})
</script>
</html>
全局注冊和局部注冊
調(diào)用Vue.component()注冊組件時,組件的注冊是全局的,這意味著該組件可以在任意Vue示例下使用。
如果不需要全局注冊,或者是讓組件使用在其它組件內(nèi),可以用選項對象的components屬性實現(xiàn)局部注冊。
上面的示例可以改為局部注冊的方式:
<!DOCTYPE html>
<html>
<body>
<div id="app">
<!-- 3. my-component只能在#app下使用-->
<my-component></my-component>
</div>
</body>
<script src="js/vue.js"></script>
<script>
// 1.創(chuàng)建一個組件構(gòu)造器
var myComponent = Vue.extend({
template: '<div>This is my first component!</div>'
})
new Vue({
el: '#app',
components: {
// 2. 將myComponent組件注冊到Vue實例下
'my-component' : myComponent
}
});
</script>
</html>
由于my-component組件是注冊在#app元素對應(yīng)的Vue實例下的,所以它不能在其它Vue實例下使用。
<div id="app2">
<!-- 不能使用my-component組件,因為my-component是一個局部組件,它屬于#app-->
<my-component></my-component>
</div>
<script>
new Vue({
el: '#app2'
});
</script>
如果你這樣做了,瀏覽器會提示一個錯誤。

//注冊組件(全局 component)
Vue.component("my-component",{
template:'<div>這是一個全局組件測試 </div>'
});
new Vue({
el:"#app5"
})
//(局部components)
new Vue({
el:"#app6",
components:{
'test-component':{
template:"<div>這是一個局部的組件測試</div>"
}
}
});
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于net?6+vue?插件axios?后端接收參數(shù)問題
接到這樣一個項目需求是這樣的,前端vue?必須對象傳遞后端也必須對象接收,接下來通過本文給大家介紹下net?6+vue?插件axios?后端接收參數(shù)的問題,需要的朋友可以參考下2022-01-01
使用VUE和webrtc-streamer實現(xiàn)實時視頻播放(監(jiān)控設(shè)備-rtsp)
WebRTC-streamer是一項使用簡單機制通過WebRTC流式傳輸視頻捕獲設(shè)備和RTSP源的實驗,下面這篇文章主要給大家介紹了關(guān)于如何使用VUE和webrtc-streamer實現(xiàn)實時視頻播放(監(jiān)控設(shè)備-rtsp)的相關(guān)資料,需要的朋友可以參考下2022-11-11
Vue.js+express利用切片實現(xiàn)大文件斷點續(xù)傳
斷點續(xù)傳就是要從文件已經(jīng)下載的地方開始繼續(xù)下載,本文主要介紹了Vue.js+express利用切片實現(xiàn)大文件斷點續(xù)傳,具有一定的參考價值,感興趣的可以了解下2023-05-05

