Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

09 支持插件 #11

Open
xwjie opened this issue Jan 13, 2018 · 0 comments
Open

09 支持插件 #11

xwjie opened this issue Jan 13, 2018 · 0 comments

Comments

@xwjie
Copy link
Owner

xwjie commented Jan 13, 2018

实现

比较简单,增加一个全局变更,增加一个static方法use

主要是函数 apply用法

// 全局插件
const globalPlugins: Array<Function | Object> = []

class Xiao{

  // 其他代码

  /**
   * 插件安装指令
   */
  // D:\OutPut\VUE\vue\src\core\global-api\use.js
  static use(plugin: Function | Object) {
    if (globalPlugins.indexOf(plugin) > -1) {
      return
    }

    //fixme additional parameters
    const args = toArray(arguments, 1)
    args.unshift(Xiao) //放到第一个位置

    if (typeof plugin.install === 'function') {
      plugin.install.apply(Xiao, args)
    } else if (typeof plugin === 'function') {
      plugin.apply(Xiao, args)
    }

    globalPlugins.push(plugin)
  }
}

插件可以直接传入一个函数,或者一个包含 install 函数的对象。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant