导航
install(Vue, options) 方法install 方法思路:
Vue.component(component.name, component)my-ui/index.js
import Select from './Select';
import Link from './Link';
const COMPONENTS = [
Select,
Link
];
const MyUI = {};
MyUI.install = function(Vue, options) {
COMPONENTS.forEach(component => {
Vue.component(component.name, component);
});
}
export default MyUI;
main.js 使用
import MyUI from './my-ui';
Vue.use(MyUI, {
}); // 这里的第二个参数: {} 作为整体,就是 options 接收
options.components 是否有值
Vue.component(...)export { MySelect, MyInput }export default MyUImy-ui/index.js
import Select from './Select';
import Link from './Link';
const COMPONENTS = [
Select,
Link
];
const MyUI = {};
MyUI.install = function(Vue, options) {
if (options && options.components) {
const components = options.components;
components.forEach(componentName => {
COMPONENTS.forEach(component => {
if (componentName === component.name) {
Vue.component(component.name, component);
}
});
});
} else {
COMPONENTS.forEach(component => {
Vue.component(component.name, component);
});
}
}
export default MyUI;
main.js 使用
import MyUI from './my-ui';
Vue.use(MyUI, {
components: [
'MyButton',
'MyInput'
]
});
import Select from './Select';
import Link from './Link';
const COMPONENTS = [
Select,
Link
];
const MyUI = {};
const MySelect = {};
const MyLink = {};
MySelect.install = Vue => Vue.component(Select.name, Select);
MyLink.install = Vue => Vue.component(Link.name, Link);
export {
MySelect,
MyLink
}
MyUI.install = function(Vue, options) {
if (options && options.components) {
const components = options.components;
components.forEach(componentName => {
COMPONENTS.forEach(component => {
if (componentName === component.name) {
Vue.component(component.name, component);
}
});
});
} else {
COMPONENTS.forEach(component => {
Vue.component(component.name, component);
});
}
}
export default MyUI;
使用:
import { MySelect, MyLink } from './my-ui';
Vue.use(MySelect);