<div id="EmailInput" v-dir-email.needat="emailAddress"> <input type="text" v-model="emailAddress"> <ul> <li v-for="email in emails | filterBy host" @click="setEmail(email)">{{email}}</li> </ul></div>現(xiàn)在指令跟實(shí)例是分開的,但是邏輯上,它們應(yīng)該是一個整體才對,應(yīng)該怎么寫才能把它們兩個整合到一起呢?是把指令里邊的東西,寫在實(shí)例里邊還是怎么樣呢?我把實(shí)例中的data移動到directive里邊,刷新沒問題,往里一打值就報錯。我在bind鉤子上使用this.vm.$set也是報錯,都報Vue warn: You are setting a non-existent path "emailAddress" on a vm instance. Consider pre-initializing the property with the "data" option for more reliable reactivity and better performance.好像是說無法在實(shí)例上設(shè)置不存在的數(shù)據(jù)。我找到方法了,是在directive的bind中使用Vue.set(),把屬性設(shè)置進(jìn)去。但是為什么不能用this.vm.$set,教程里邊明明寫著可以用這兩種方法,但是使用后者總報上邊那個錯(設(shè)置不存在的路徑錯誤)。
怎樣寫這個指令,讓它看起來像是一個整體?
犯罪嫌疑人X
2018-08-14 10:09:07