Vue.use() 实现组件注册

99 阅读1分钟
  1. 一般我们注册组件都在入口文件main.js中 使用Vue.component(组件名,组件)注册

image.png

弊端 :

  • 如果组件过多无法保持main.js文件的简洁

解决方法 : 抽离组件注册

1.在公共组件component中新建index.js

image.png 2. 在main.js文件中引入 image.png

3.这样就可以在component 中注册多个组件了

image.png

二 . 使用Vue.use() 注册组件

  • 了解 Vue.use()

image.png

  • 在main.js中引入 index.js文件 使用 Vue.use() 将文件以参数传入

image.png

  • 注册全局组件 使用 install(vue) 注册组件

    • Vue.use() 实现原理

      • 需要传入一个参数对象||函数
      • 如果参数是对象提供了 install()函数
      • install() 函数接收的参数是 Vue
      • 使用 Vue.component() 注册

image.png