Comment on page
Plugin
Packhouse Plugin
Packhouse的Plugin只能存在於實體化的對象,沒有全局註冊的行為。
const Order = require('packhouse/plugins/Order')
const Packhouse = require('packhouse')
const packhouse = new Packhouse()
packhouse.plugin(Order)
console.log(packhouse.order()) // Order
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.
當Packhouse偵測到相同的Class對象,則不會有任何反應。
packhouse.plugin(Order)
// 以下行為不會觸發任何事
packhouse.plugin(Order)
這意味著當你基於外部開發時引入某些Plugin時能有更多元的註冊行為:
const group = {
install(group, options, packhouse) {
packhouse.plugin(require('packhouse/plugins/Order'))
}
}
Last modified 3yr ago