Packhouse
  • Introduction
  • Versions
  • Core
    • Group
      • Mold
      • Tool
      • Line
    • Invoke
    • Utils
    • Event
    • Merger
    • Plugin
    • Main
    • Intercept Error
  • Plugins
    • Step
    • Order
  • Tooling
    • Unit Test
  • Application
    • API Service
  • From
    • GitHub
    • Metal Sheep
Powered by GitBook
On this page
  • Develop
  • Repeat Registration Of Plugins

Was this helpful?

  1. Core

Plugin

Packhouse Plugin

PreviousMergerNextMain

Last updated 5 years ago

Was this helpful?

Packhouse的Plugin只能存在於實體化的對象,沒有全局註冊的行為。

是我們提供的套件 ,也是一個簡單的開發範例。

我們可以藉由我們不推薦你直接使用plugin,請參閱正規化方法。

const Order = require('packhouse/plugins/Order')
const Packhouse = require('packhouse')
const packhouse = new Packhouse()

packhouse.plugin(Order)

console.log(packhouse.order()) // Order

Develop

Plugin是一個class,會在constructor的第一個參數中接收到實體化的Packhouse對象。

class MyPlugin {
    constructor(packhouse) {
        packhouse.hello = function() {
            console.log(`hello world.`)
        }
    }
}

const Packhouse = require('packhouse')
const packhouse = new Packhouse()

packhouse.plugin(MyPlugin)
packhouse.hello() // hello world.

Repeat Registration Of Plugins

當Packhouse偵測到相同的Class對象,則不會有任何反應。

packhouse.plugin(Order)
// 以下行為不會觸發任何事
packhouse.plugin(Order)

這意味著當你基於外部開發時引入某些Plugin時能有更多元的註冊行為:

const group = {
    install(group, options, packhouse) {
        packhouse.plugin(require('packhouse/plugins/Order'))
    }
}
Order
Main