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