Vue動態(tài)組件與內(nèi)置組件淺析講解
一、動態(tài)組件
在vue中,有很多的組件可以掛載同一個掛載點上面,要在同一個掛載的點上的多個組件之間可以實現(xiàn)動態(tài)的切換渲染,我們可以通過內(nèi)置組件component的is屬性動態(tài)的綁定組件,然后我們就可以根據(jù)is的值來決定哪一個組件要被渲染,非常的方便。
我們通過一點簡單的實例代碼可以加深了解:
示例代碼:
<!DOCTYPE html> <html lang="en"> <head> <title>組件之間的傳遞</title> </head> <body> <div id="app"> <h1>小小閑置網(wǎng)</h1> <input type="radio" name="tab" value="qiubite1" v-model="cfl">王者賬號: <img src="C:\Users\Administrator\Desktop\李寶\wangzhe.jpg" alt="" style="width: 30px;height:30px"> <input type="radio" name="tab" value="qiubite2" v-model="cfl">電話: <input type="radio" name="tab" value="qiubite3" v-model="cfl">估價: <component v-bind:is="cfl"></component> </component> </div> <template id="n1"> <div style="width: 200px;height: 200px;border: 2px solid rgb(100, 100, 196);"> <h1>賬號</h1> <input type="text" placeholder="輸入你的賬號:"> </div> </template> <template id="n2"> <div style="width: 200px;height: 200px;border: 2px solid rgb(100, 100, 196);"> <h1>電話</h1> <input type="text" placeholder="輸入你的電話:"> </div> </template> <template id="n3"> <div style="width: 200px;height: 200px;border: 2px solid rgb(100, 100, 196);"> <h1>估價:</h1> <input type="text" placeholder="你心儀賣出的價格:"> </div> </template> <script src="http://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script> <script> var vm = new Vue({ el:"#app", data:{cfl:"qiubite1"}, components:{ 'qiubite1':{template:'#n1'}, 'qiubite2':{template:'#n2'}, 'qiubite3':{template:'#n3'}, } }) </script> </body> </html>
運行結(jié)果:
我們可以看到三個按鈕的value的值設置成了組件的名字,雙向綁定cfl(懲罰陸,沒什么含義,自己亂起的)數(shù)據(jù),單擊按鈕,就可以改變value的值從而更新cfl里面的值;component組件的is屬性動態(tài)的綁定了cfl里面的值,根據(jù)這個is就知道哪個組件被渲染了。
二、內(nèi)置組件
根據(jù)上面的實例結(jié)果,我們看到了輸入框里輸入數(shù)據(jù),當你切換到別的組件的時候,原來組件的數(shù)據(jù)不會被保存,所以內(nèi)置組件可以包裹我們的動態(tài)組件,會將往期的組件進行緩存,而不是銷毀,他會把切換回去的組件緩存起來,做到保留組件狀態(tài)。
實例代碼:
<!DOCTYPE html> <html lang="en"> <head> <title>組件之間的傳遞</title> </head> <body> <div id="app"> <h1>小小閑置網(wǎng)</h1> <input type="radio" name="tab" value="qiubite1" v-model="cfl">王者賬號: <img src="C:\Users\Administrator\Desktop\李寶\wangzhe.jpg" alt="" style="width: 30px;height:30px"> <input type="radio" name="tab" value="qiubite2" v-model="cfl">電話: <input type="radio" name="tab" value="qiubite3" v-model="cfl">估價: <keep-alive><component v-bind:is="cfl"></component></keep-alive> </component> </div> <template id="n1"> <div style="width: 200px;height: 200px;border: 2px solid rgb(100, 100, 196);"> <h1>賬號</h1> <input type="text" placeholder="輸入你的賬號:"> </div> </template> <template id="n2"> <div style="width: 200px;height: 200px;border: 2px solid rgb(100, 100, 196);"> <h1>電話</h1> <input type="text" placeholder="輸入你的電話:"> </div> </template> <template id="n3"> <div style="width: 200px;height: 200px;border: 2px solid rgb(100, 100, 196);"> <h1>估價:</h1> <input type="text" placeholder="你心儀賣出的價格:"> </div> </template> <script src="http://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script> <script> var vm = new Vue({ el:"#app", data:{cfl:"qiubite1"}, components:{ 'qiubite1':{template:'#n1'}, 'qiubite2':{template:'#n2'}, 'qiubite3':{template:'#n3'}, } }) </script> </body> </html>
運行結(jié)果:
以上就是Vue動態(tài)組件與內(nèi)置組件淺析講解的詳細內(nèi)容,更多關(guān)于Vue動態(tài)組件與內(nèi)置組件的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
淺談Vue.js中ref ($refs)用法舉例總結(jié)
本篇文章主要介紹了淺談Vue.js中ref ($refs)用法舉例總結(jié),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12vue實現(xiàn)簡易圖片左右旋轉(zhuǎn),上一張,下一張組件案例
這篇文章主要介紹了vue實現(xiàn)簡易圖片左右旋轉(zhuǎn),上一張,下一張組件案例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07