Vue組件是學習Vue框架最比較難的部分,而這部分難點我認爲能夠分爲三個部分學習,即javascript
因此將用三篇博客分別進行介紹以上三種狀況和使用html
Vue的設計者,對組件和父組件之間的關係流上作了闡述,即單向數據流圖:父組件向子組件傳遞數據,子組件回饋事件java
組件意味着協同工做,一般父子組件會是這樣的關係:組件 A 在它的模板中使用了組件 B。它們之間必然須要相互通訊:父組件要給子組件傳遞數據,子組件須要將它內部發生的事情告知給父組件。然而,在一個良好定義的接口中儘量將父子組件解耦是很重要的。這保證了每一個組件能夠在相對隔離的環境中書寫和理解,也大幅提升了組件的可維護性和可重用性。git
在 Vue 中,父子組件的關係能夠總結爲 props down, events up。父組件經過 props 向下傳遞數據給子組件,子組件經過 events 給父組件發送消息。看看它們是怎麼工做的。github
上文提到的三篇文章,都使用一個父組件掛載對象,內容比較長(能夠選擇不看,直接看props的使用),感興趣能夠到git上去看源代碼數組
模版:框架
<body>
<div id="el-component-id"></div> <body
Vue實例:函數
var vm = new Vue({ el: "#el-component-id", data: { welcome: "welcome to Vue", parentMessage: "this is parent message", iMessage: "", person: { name: "小明", from: "江蘇", to: "江西", purpose: "喝一杯牛奶" }, persons: 10, sumOfTotal: 0, nativeSumOfTotal: 0, food: "牛肉", languages: ["英語", "中文", "希臘語", "法語", "俄羅斯語"], dynamicComponent: "AppHeader" }, methods: { incrementWithTotal: function() { this.sumOfTotal = this.sumOfTotal + 1; }, nativeDoThing: function() { this.nativeSumOfTotal += 1; }, changeCurrentComponent: function() { let components = ["AppHeader", "AppFooter", "AppMain"]; let idx = components.indexOf(this.dynamicComponent); if (idx == 2 || idx == -1) { idx = 0; } else { ++idx; } this.dynamicComponent = components[idx]; } }, components: { AppHeader: { props: ["initialText"], template: "<div><strong>{{title}}</strong></div>", data: function() { return { title: this.initialText } } }, AppFooter: { props: ["initialText"], template: "<div><sub>{{footerTitle}}</sub></div>", data: function() { return { footerTitle: this.initialText } } }, AppMain: { props: ["initialText"], template: "<div style='color:blue;'>{{mainContent}}</div>", data: function() { return { mainContent: this.initialText } } } } });
組件定義:學習
// 使用props數組的形式進行傳遞參數 Vue.component("component-span-child-1", { props: ["message"], template: "<span>{{message}}</span>" })
模版中進行傳值:
<div> <h4>組件一-props傳遞單個參數</h4> // 字面量傳值 <component-span-child-1 message="component-style-one"></component-span-child-1> <br /> // 綁定父組件對象實例屬性 v-bind:someProperty簡寫爲:someProperty <component-span-child-1 :message="parentMessage"></component-span-child-1> <br /> <component-span-child-1 v-bind:message="parentMessage"></component-span-child-1> <br /> <input v-model="iMessage" placeholder="請輸入值"/> <component-span-child-1 :message="iMessage"></component-span-child-1> </div>
組件定義:
Vue.component("component-span-child-2", { props: ["name", "from", "to", "purpose"], template: "<div><span>{{name}}從{{from}}到{{to}},{{purpose}}</span></div>" })
模版中傳值:
<div>
<h4>組件二-props傳遞多個參數</h4> // 字面量傳值 <component-span-child-2 name="小李" from="南京" to="北京" purpose="去買個書包"></component-span-child-2> // 父組件實例對象屬性傳值 <component-span-child-2 :name="person.name" :from="person.from" :to="person.to" :purpose="person.purpose"></component-span-child-2> </div>
組件定義:
能夠校驗傳遞進來的屬性,例如:1. 校驗類型 2. 是否必須傳遞 3. 提供默認值 4. 經過函數校驗,如校驗Number類型是否大於某個值
Vue.component("component-span-child-3", { props: { name: { type: String, require: true }, persons: { type: Number, default: 1, validator: function(value) { return value > 0; } }, location: { type: String, default: "上海" }, action: { type: String, default: "拉粑粑" } }, template: "<div><span>{{name}}和{{persons}}我的,去{{location}}裏面{{action}}</span></div>" })
模版中使用:
<div>
<h4>組件三-使用props對象傳遞參數,和校驗</h4> <component-span-child-3 name="小狗" :persons="persons" location="講述郾城" action="去淘金啊"></component-span-child-3> </div>
父組件向子組件主要是經過props關鍵字,主要使用狀況能夠分爲上面描述的三種。props的封裝能夠是一個數組,也能夠是對象。
熟練掌握父組件向子組件傳遞參數的方法,能夠對Vue的關鍵部分更快的理解。