詳解vue父子模版嵌套案例
更新時間:2017年03月04日 15:33:44 作者:____雨歇微涼
本篇文章主要介紹了詳解vue父子模版嵌套案例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
這里是父子模版的調用
這里是針對于vue1.0,如果要學2.0,建議大家去看官方文檔
vue2.0 :http://vuefe.cn/guide/
vue-router2.0: https://router.vuejs.org/zh-cn/essentials/getting-started.html
第一種,子組件模版直接寫在js里
//定義模版掛載點my-component <div id="exampleBox1"> <com-ponent></com-ponent> </div> <script src="../vue/node_modules/vue/dist/vue.js"></script> <script> var Component = Vue.extend({// 定義 template: '<div>A custom component!</div>', data: function () { return { name: 'yuxie' } } }); Vue.component('com-ponent', Component);// 注冊 //注意,extend(json) 和 vue.component('com-ponent', json)//這兩個JSON是相等的。 //所以下面第二種會將extend()函數(shù)省略掉,直接在component中定義,系統(tǒng)會自動調用extend函數(shù)。 var conp = new Vue({// 創(chuàng)建根實例 el: '#exampleBox1' }); </script>
第二種,使用HTML模版
<!-- 父組件模板 --> <div id="exampleBox2" style="border:1px solid #ccc;width:500px;"> <div>{{parent.name}}</div> <!--模版掛載標識--> <children></children> </div> <!-- 子組件模板 --> <template id="child-template"> <p style="background:#eee;">{{text}}</p> </template> <script> Vue.component('children', {//child是模版掛載的標簽名 template: '#child-template',//id對應子組件的ID data: function () { return { text: '這里是子組件的內容' } } }); var parent = new Vue({// 初始化父組件 el: '#exampleBox2', data: { parent: { name:'這里是父組件的內容' } } }) </script>
第三種、來一個復雜的
<div id="example"> <!-- 所有的模板掛件,都必須在根實例ID內部,否則找不到掛件 --> <my-component></my-component> <!-- 模版可以重用多次 ···· 只不過一樣的東西沒有這個必要 --> <child></child>復用一次 <child></child>復用二次 <child></child> ··· <child></child> ··· </div> <!--比如放在這里是找不到的--> <child></child> <script src="../vue/node_modules/vue/dist/vue.js"></script> <script> //定義子組件,子組件必須在父組件之前定義。 var Child = Vue.extend({template: '<div>A child component!</div>'}); //定義父組件 var Parent = Vue.extend({ template: '<div style="border: 1px solid #ccc;width:200px;">Parent<child-component></child-component>父模版內部</div>', components: { // 調用子組件 'child-component': Child } }); // 注冊父組件 Vue.component('my-component', Parent); //復用子組件。 Vue.component('child', Child); // 創(chuàng)建根實例,所有組件都需要在根實例之前創(chuàng)建。 new Vue({ el: '#example' }) </script>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Vue父子模版?zhèn)髦导敖M件傳值的三種方法
- 詳解用vue2.x版本+adminLTE開源框架搭建后臺應用模版
- vue Element-ui input 遠程搜索與修改建議顯示模版的示例代碼
- VSCode寫vue項目一鍵生成.vue模版,修改定義其他模板的方法
- 詳解如何用VUE寫一個多用模態(tài)框組件模版
- 詳解vue 模版組件的三種用法
- vue19 組建 Vue.extend component、組件模版、動態(tài)組件 的實例代碼
- Vue 中可以定義組件模版的幾種方式
- 解決vue與node模版引擎的渲染標記{{}}(雙花括號)沖突問題
- Vue2?模版指令元素綁定事件執(zhí)行順序解析
- vue模版編譯詳情
- vue的指令和插值問題匯總
- vue.js模版插值的原理與實現(xiàn)方法簡析
相關文章
一步步教你搭建VUE+VScode+elementUI開發(fā)環(huán)境
這篇文章主要給大家介紹了關于搭建VUE+VScode+elementUI開發(fā)環(huán)境的相關資料,近期被配置環(huán)境的事情弄得整個人都要炸了,現(xiàn)在整理如下,希望有相同需求的朋友可以不用走彎路,需要的朋友可以參考下2023-07-07關于Vue中echarts響應式頁面變化resize()的用法介紹
Vue項目中開發(fā)數(shù)據(jù)大屏,使用echarts圖表根據(jù)不同尺寸的屏幕進行適配,resize()可以調用echarts中內置的resize函數(shù)進行自適應縮放,本文將給大家詳細介紹resize()的用法,需要的朋友可以參考下2023-06-06