首页 > 其他 > 详细

Vue 一个组件引用另一个组件

时间:2018-05-07 18:25:23      阅读:269      评论:0      收藏:0      [点我收藏+]

有些时候需要这么做,比如,我想在首页加载轮播组件,但是又不想全局注册(因为不是每个页面都需要轮播功能)

方法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> 

 

Vue 一个组件引用另一个组件

原文:https://www.cnblogs.com/ming-os9/p/9004071.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!