vue中怎么實現(xiàn)父子模版嵌套

這篇文章將為大家詳細講解有關vue中怎么實現(xiàn)父子模版嵌套,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。

站在用戶的角度思考問題,與客戶深入溝通,找到東川網(wǎng)站設計與東川網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設計與互聯(lián)網(wǎng)技術結合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:網(wǎng)站制作、網(wǎng)站設計、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣、域名注冊、網(wǎng)絡空間、企業(yè)郵箱。業(yè)務覆蓋東川地區(qū)。

第一種,子組件模版直接寫在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)會自動調(diào)用extend函數(shù)。
  var conp = new Vue({// 創(chuàng)建根實例
    el: '#exampleBox1' 
   });
</script>

第二種,使用HTML模版

<!-- 父組件模板 -->
<div id="exampleBox2" >
  <div>{{parent.name}}</div>
  <!--模版掛載標識-->
  <children></children>
</div>
<!-- 子組件模板 -->
<template id="child-template"> 
  <p >{{text}}</p>
</template>
<script> 
Vue.component('children', {//child是模版掛載的標簽名    
   template: '#child-template',//id對應子組件的ID    
   data: function () {      
     return {        
       text: '這里是子組件的內(nèi)容'      
     }    
   }  
});  
var parent = new Vue({// 初始化父組件    
    el: '#exampleBox2',    
    data: {      
      parent: {        
         name:'這里是父組件的內(nèi)容'      
      }      
     }  
 })
</script>

第三種、來一個復雜的

<div id="example">
  <!--  所有的模板掛件,都必須在根實例ID內(nèi)部,否則找不到掛件  -->
  <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 >Parent<child-component></child-component>父模版內(nèi)部</div>', 
     components: {
       // 調(diào)用子組件 
       'child-component': Child 
     } 
  }); 
  // 注冊父組件 
  Vue.component('my-component', Parent);
  //復用子組件。
  Vue.component('child', Child); 
  // 創(chuàng)建根實例,所有組件都需要在根實例之前創(chuàng)建。
  new Vue({ 
    el: '#example' 
  })
</script>

關于vue中怎么實現(xiàn)父子模版嵌套就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

網(wǎng)站欄目:vue中怎么實現(xiàn)父子模版嵌套
文章鏈接:http://muchs.cn/article6/pdggog.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設、網(wǎng)站建設標簽優(yōu)化、關鍵詞優(yōu)化、微信公眾號、虛擬主機

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設