vue全局組件和局部組件的寫法介紹
全局組件和局部組件寫法
vue組件有兩種,一種是全局組件,一種是局部組件。整個(gè)項(xiàng)目經(jīng)常用到的用全局寫法,用到比較少的專供特定頁面用的使用局部組件。
全局組件引入寫法
在項(xiàng)目的main.js中:
import Vue from 'vue';
import MyComponent from '@/components/MyComponent.vue'; // 導(dǎo)入自己寫的組件文件
?
Vue.use(MyComponent); // 自定義全局組件的時(shí)候需要Vue.use();
Vue.component('my-component', MyComponent); //初始化組件
?
new Vue({
? el: '#app',
? router,
? components: {
? ? App,
? ? MyComponent
? },
? template: '<App/>',
});?局部組件引入寫法
在需要使用組件的a.vue文件中:
<template>
</template>
?
<script>
import MyComponent from '@/components/MyComponent.vue';
export default {
? components: {MyComponent}, // 直接初始化就可以啦,不需要Vue.use();
? data() {},
? methods: {}
};
</script>
?
<style lang="scss" scoped>
</style>?下面附上自定義組件的MyComponent.vue文件代碼:
<template>
? <div>
? ? <a @click="methods1()"></a>
? </div>
</template>
<script>
import { MessageBox } from 'mint-ui';
export default {
? data () { // 組件內(nèi)參數(shù)定義在data中
? ? return {
? ? ? data1: {}
? ? };
? },
? props: { // 組件傳參使用props
? ? value1: String,
? ? value2: Number
? },
? methods: { // 組件的計(jì)算方法
? ? methods1 () {
? ? }
? }
};
</script>
<style lang="scss" scoped>
</style>vue全局/局部組件
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script src="js/vue.js"></script>
</head>
<body>
<!-- 全局組件簡寫-->
<div id="example1">
<my-compoent></my-compoent>
</div>
<script>
Vue.component('my-compoent',{
template:'<div>測試1</div>'
})
new Vue({
el:'#example1'
})
</script>
<!-- 注冊全局組件-->
<div id="example2">
<my-compoent1></my-compoent1>
</div>
<script>
//創(chuàng)建一個(gè)組件構(gòu)造器
var myComponent = Vue.extend({
template:'<div> 測試2</div>'
})
//注冊全局組件: (z組件名稱 組件構(gòu)造器)
//Vue.component(tagName,options)
Vue.component('my-compoent1',myComponent)
new Vue({
el:'#example2'
})
</script>
<!-- 注冊局部組件-->
<div id="example3">
<my-compoent></my-compoent>
</div>
<div id="example4">
<my-compoent></my-compoent>
</div>
<script>
//創(chuàng)建一個(gè)組件構(gòu)造器
var myComponent = Vue.extend({
template:'<div> 局部組件4</div>'
})
//注冊組件 并指定組件的標(biāo)簽 逐漸的html標(biāo)簽為 my-compoent
Vue.component('my-compoent',myComponent)
new Vue({
el:'#example4',
components:{
'my-component':myComponent
}
})
</script>
<!-- 父子組件 數(shù)據(jù)傳遞
父子組件的關(guān)系:通常組件A在它的模板中使用組件B,此時(shí)組件A為父組件,組件B為子組件。父子組件應(yīng)該解耦,
組件實(shí)例的作用域是孤立的,子組件中不能直接使用父組件的數(shù)據(jù)。應(yīng)該使用props傳遞父組件到子組件的數(shù)據(jù),
子組件通過events給父組件發(fā)消息,以此實(shí)現(xiàn)父子組件間的通信。
如上,在其他組件內(nèi)部用components聲明組件,即為局部注冊。在Vue實(shí)例中用components注冊組件時(shí),
可以理解為Vue實(shí)例為一個(gè)大的父組件,其他任何注冊的組件都是子組件。所以在注冊組件時(shí),
如果要使用Vue實(shí)例data中的數(shù)據(jù),都要用props傳遞Vue實(shí)例中的數(shù)據(jù),讓Vue實(shí)例的數(shù)據(jù)在組件中可用。
還可以用v-bind動態(tài)綁定props的值到父組件的數(shù)據(jù),父組件數(shù)據(jù)發(fā)生變化時(shí),子組件的數(shù)據(jù)也相應(yīng)的變化。
父--》子:父組件通過props傳遞父組件到子組件
子--》父:子組件通過events給父組件發(fā)消息
-->
<div id="test">
<template id="testProp">
<ul>
<li v-for="book in books">
<p>{{book.title}}</p>
<p>{{book.desc}}</p>
<p>{{book.author}}</p>
</li>
</ul>
<template>
<test-prop :book-list = "books"></test-prop>
</div>
<script>
var TestProp = Vue.extend({
template:'#testProp',
props: ['book-list']
});
var test = new Vue({
el: '#test',
data: function(){
return {
books: [
{
title: 'title1',
desc: 'desc1',
author: 'author1'
},
{
title: 'title2',
desc: 'desc2',
author: 'author2'
},
{
title: 'title3',
desc: 'desc3',
author: 'author3'
},
],
}
},
components:{
'test-prop': TestProp,
},
});
</script>
</body>
</html>以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue使用axios?post發(fā)送json數(shù)據(jù)跨域請求403的解決方案
這篇文章主要介紹了vue使用axios?post發(fā)送json數(shù)據(jù)跨域請求403的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12
vue項(xiàng)目中canvas實(shí)現(xiàn)截圖功能
這篇文章主要為大家詳細(xì)介紹了vue項(xiàng)目中canvas實(shí)現(xiàn)截圖功能,截取圖片的一部分,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07
vue3通過ref獲取子組件defineExpose的數(shù)據(jù)和方法
defineExpose是Vue3中新增的選項(xiàng),用于向父組件暴露子組件內(nèi)部的屬性和方法,通過defineExpose,子組件可以主動控制哪些屬性和方法可以被父組件訪問,本文主要介紹了vue3通過ref獲取子組件defineExpose的數(shù)據(jù)和方法,需要的朋友可以參考下2023-10-10
解決運(yùn)行vue項(xiàng)目內(nèi)存溢出問題
這篇文章主要介紹了解決運(yùn)行vue項(xiàng)目內(nèi)存溢出問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04
vue.js高德地圖實(shí)現(xiàn)熱點(diǎn)圖代碼實(shí)例
這篇文章主要介紹了vue.js高德地圖實(shí)現(xiàn)熱點(diǎn)圖,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04

