插件

问题:插件有什么好处?

可以一次性将插件安装到vue中的程序里面去,使用的时候use即可

使用

Vue.use在入口文件中执行

  1. Vue.use(MyPlugin, { someOption: true });

开发

Vue.js 的插件应该暴露一个 install 方法。这个方法的第一个参数是 Vue 构造器,第二个参数是一个可选的选项对象:

  1. MyUI.install = function (vue, options) {
  2. Vue.component
  3. }

注册

  1. //全局注册
  2. Vue.use(MyUI, {
  3. components: [
  4. 'MyButton',
  5. 'MyInput'
  6. ]
  7. });
  1. /**
  2. * 组件开发的install方法
  3. * @param {*} Vue vm实例
  4. * @param {*} options 组件对象
  5. */
  6. MyUI.install = function (Vue, options) {
  7. //用户有输入组件代码时才执行
  8. if (options && options.components) {
  9. const components = options.components;
  10. // console.log(components);
  11. //['MyButton', 'MyInput']
  12. //遍历组件对象
  13. //获取组件对象里的组件名称
  14. components.forEach((componentName) => {
  15. //遍历组件池
  16. //获取自定义的组件对象
  17. COMPONENTS.forEach((component) => {
  18. // console.log(component);
  19. //{name: 'MyButton', props: {…}, _compiled: true, render: ƒ, …}
  20. //组件名称一致时说明找到该组件
  21. if (componentName === component.name) {
  22. //注册组件
  23. //Vue.component(组件名称, 组件对象)
  24. Vue.component(component.name, component);
  25. }
  26. });
  27. });
  28. } else {
  29. //用户没有有输入组件代码时才执行
  30. COMPONENTS.forEach((component) => {
  31. //注册组件
  32. Vue.component(component.name, component);
  33. });
  34. }
  35. }

按需加载组件

入口文件导入

  1. import {MyButton,MyInput} from './xxx';

Vue.use加载使用

  1. Vue.use(MyButton);
  2. Vue.use(MyInput);

在插件入口文件注册组件对象

  1. //1.注册按需加载的组件对象
  2. const MyButton = {};
  3. const MyInput = {};
  4. //2.导入组件对象
  5. import Button from './Button';
  6. import Input from './Input';
  7. //3.分别给组件对象注册
  8. MyButton.install = function (Vue) {
  9. Vue.component(Button.name, Button);
  10. }
  11. MyInput.install = function (Vue) {
  12. Vue.component(Input.name, Input);
  13. }
  14. //4.导出组件对象
  15. export {
  16. MyButton,
  17. MyInput
  18. }

my-UI

编写一个UI插件库

技术:vue2

  1. //项目目录:

目前已经编写的UI组件有:

  • button:按钮

    • 配置项:type="danger|warning|success|primary"
  • input:输入框
  • link:文字链接

    • 配置项:
    • herf="xxx"
    • type="danger|warning|success|primary"
    • target="_blank"
  • select:下拉菜单

    • 配置项:data="[...]"
  • stars:五星好评

    • 配置项:
    • num="3"
    • size="30"
  • magnifier:放大镜

    • 配置项:
    • link="xxx"
    • blank="true"
    • imgAlt="model"
    • imgWidth="375"
    • imgHeight="481"
    • magWidth="100"
    • magHeight="100"
    • maginiferImgData="[...]"
  • carousel:无缝轮播图

    • 配置项:
    • autoplay="true"
    • duration="3000"
    • initial="0"
    • hasDot="true"
    • hasDirector="true"
    • dotBgColor="orange"
    • autoplayDir="'prev'"
    • courouselImgData="[...]"
  • tree-menu:树形左侧菜单

    • 配置项:
    • 用户提供的树状数组数据