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