Vue 一個組件引用另一個組件


有些時候需要這么做,比如,我想在首頁加載輪播組件,但是又不想全局注冊(因為不是每個頁面都需要輪播功能)

方法1:

 1 <template>  
 2     <div>  
 3         <!-- 3.在template中就可以直接使用了 -->  
 4         <testComponent></testComponent>  
 5     </div>  
 6 </template>  
 7   
 8 <script>  
 9     //1.先使用import導入你要在該組件中使用的子組件  
10     import testComponent from './testComponent.vue'  
11     export default {  
12         //2.然后,在components中寫入子組件  
13         components: {testComponent},  
14         methods: {},  
15     }  
16 </script>  
17   
18 <style></style>  

方法2:

 1 <template>  
 2     <div>  
 3         <!-- 2.在template中使用 -->  
 4         <testComponent></testComponent>  
 5     </div>  
 6 </template>  
 7   
 8 <script>  
 9     export default {  
10         //1.直接在components中寫入子組件  
11         components: {  
12             testComponent:require('./testComponent.vue').default  
13         },  
14         methods: {},  
15     }  
16 </script>  
17   
18 <style></style> 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM