看一个项目的源码我个人认为首先要看的是项目组织结构,而不是直接去关注细节实现。
项目的入口是src目录下的index.js,而具体的组件/插件实现在packages目录下。
在入口文件中,首先导入各个组件,然后封装了一个install方法全局注册这些组件/插件:
const install = function(Vue, opts = {}) {
if (install.installed) return;
locale.use(opts.locale);
locale.i18n(opts.i18n);
components.map(component => {
Vue.component(component.name, component);
});
Vue.use(Loading.directive);
Vue.prototype.$loading = Loading.service;
Vue.prototype.$msgbox = MessageBox;
Vue.prototype.$alert = MessageBox.alert;
Vue.prototype.$confirm = MessageBox.confirm;
Vue.prototype.$prompt = MessageBox.prompt;
Vue.prototype.$notify = Notification;
Vue.prototype.$message = Message;
};
最后导出一个对象,这个对象包含上面封装的install方法:
module.exports = {
install,
};
当我们引入整个库的时候,我们会这么写:
import Element from 'element-ui'
Vue.use(Element)
这里的Element就是index.js导出的那个包含install方法的对象,当我们使用Vue.use(Element)
时,就会调用这个install方法。