// 父組件 <my-components :commonData='commonData' @changeCommonData='changeCommonData'></my-components> changeCommonData(data){ this.commonData = data } // my-components子組件中 doSome(){ this.$emit('changeCommonData',data) // data === 2,這時父組件中的commonData已經更改成2,可是子組件的響應,要等待當前函數執行完後響應 console.log(this.commonData) // 原始值是1, }
some-props
和someProps
的命名方式,外部均可以經過some-props
和someProps
賦值,在內部只能經過this.someProps
得到
<text-document v-bind:title.sync="doc.title"></text-document> // 實際爲 <text-document v-bind:title="doc.title" v-on:update:title="doc.title = $event" ></text-document> ... watch:{ title(newTitle){ this.$emit('update:title', newTitle) } } ...
props: { propA:{ type: Number, default: 100, // 對象或數組默認值必須從一個工廠函數獲取 required: true, // 必傳 validator: function (value) { // 自定義驗證函數 // 這個值必須匹配下列字符串中的一個 return ['success', 'warning', 'danger'].indexOf(value) !== -1 } } }
function Person (firstName, lastName) { this.firstName = firstName this.lastName = lastName } Vue.component('blog-post', { props: { author: Person } }) // 類同於 propA: Number
Vue.component('my-component', { inheritAttrs: false, // ... })
<div :style="{ display: ['-webkit-box', '-ms-flexbox', 'flex'] }"></div>
v-bind=」{ title: doc.title }」
沒法正常工做<blog-post v-bind="post"></blog-post> // 等價於 <blog-post v-bind:id="post.id" v-bind:title="post.title" ></blog-post>
對於須要使用輸入法 (如中文、日文、韓文等) 的語言,你會發現 v-model 不會在輸入法組合文字過程當中獲得更新。若是你也想處理這個過程,請使用 input 事件。當用輸入法時可以捕獲輸入的每一個拼音字母vue
<div id="example-5"> <select v-model="selected"> // selected 默認爲'' <option disabled value="">請選擇</option> <option>A</option> <option>B</option> <option>C</option> </select> <span>Selected: {{ selected }}</span> </div>
<input type="radio" id="huey" name="drone" value="huey" checked> // checked初始被選中
<input type="checkbox" v-model="toggle" true-value="yes" false-value="no" >
<input type="checkbox" id="checkbox" v-model="checked">
選中時checked爲true,不然爲false。不須要value<div id='example-3'> <input type="checkbox" id="jack" value="Jack" v-model="checkedNames"> <label for="jack">Jack</label> <input type="checkbox" id="john" value="John" v-model="checkedNames"> <label for="john">John</label> </div> new Vue({ el: '#example-3', data: { checkedNames: [] //=>['Jack','John'] } })
// input中 <input v-model="searchText"> // 等價於 <input v-bind:value="searchText" v-on:input="searchText = $event.target.value" // 這裏的$event是事件對象 > // 組件中 // 組件外部使用 <custom-input v-model="searchText"></custom-input> // 等價於 <custom-input v-bind:value="searchText" v-on:input="searchText = $event" ></custom-input> // 組件內部定義 Vue.component('custom-input', { props: ['value'], template: ` <input v-bind:value="value" v-on:input="$emit('input', $event.target.value)" // $event.target指向時間對象,input對象有value屬性 > ` })
Vue.component('base-checkbox', { model: { prop: 'checked', event: 'change' }, <base-checkbox v-model="lovingVue"></base-checkbox> // 使用 v-model 的時候,這裏的 lovingVue 的值將會傳入這個名爲 checked 的 prop。同時當 <base-checkbox> 觸發一個 change 事件並附帶一個新的值的時候,這個 lovingVue 的屬性將會被更新。
// 組件 <ul> <li v-for="todo in todos" v-bind:key="todo.id" > <!-- 咱們爲每一個 todo 準備了一個插槽,--> <!-- 將 `todo` 對象做爲一個插槽的 prop 傳入。--> <slot v-bind:todo="todo"> <!-- 回退的內容 --> {{ todo.text }} </slot> </li> </ul> // 使用(獲取組件中傳出的數據) <todo-list v-bind:todos="todos"> <!-- 將 `slotProps` 定義爲插槽做用域的名字 --> <template slot-scope="slotProps"> // 這裏可使用解構語法{ todo } <!-- 爲待辦項自定義一個模板,--> <!-- 經過 `slotProps` 定製每一個待辦項。--> <span v-if="slotProps.todo.isComplete">✓</span> {{ slotProps.todo.text }} </template> </todo-list>
// 父級組件提供 'foo' var Provider = { provide: { // provide 選項應該是一個對象或返回一個對象的函數。 foo: 'bar' }, // ... } // 子組件注入 'foo' var Child = { inject: ['foo'], created () { console.log(this.foo) // => "bar" } // ... } // inject 選項應該是數組或對象(該對象的 key 是本地的綁定名,value 是搜索用的 key 或一個包含from和default屬性的對象。(對象的from 屬性是在可用的注入內容中搜索用的 key 、default 屬性是降級狀況下使用的 value)) inject:{ myFoo1:'foo', myFoo:{ from:'foo', default:'boy' // 與 prop 的默認值相似,你須要對非原始值(數組、對象)使用一個工廠方法 } }
// 組件1 import Bus from './Bus' export default { data() { return { ......... } }, methods: { .... Bus.$emit('log', 120) }, } // 組件2 import Bus from './Bus' export default { data() { return { ......... } }, mounted () { Bus.$on('log', content => { console.log(content) }); } }
import Vue from 'vue' const Bus = new Vue() var app= new Vue({ el:'#app', data:{ Bus } })