BREAKING CHANGE: The following top-level functions exported by the 'artalk' npm package have been deprecated: `Artalk.update`, `Artalk.reload`, and `Artalk.destroy`. These methods now require invocation on an instance created by either `Artalk.init` or `new Artalk`. Please utilize instance-level methods instead, such as `artalkInstance.update`. For more information, refer to [the documentation](https://artalk.js.org/guide/frontend/import-framework.html). The update was implemented to enable the concurrent creation of multiple instances, adapting to situations where Vue components are simultaneously referenced across various pages. Initially, only a singular instance was permitted to mitigate memory leak concerns. However, this proved limiting for scenarios involving the caching of multiple component instances through 'keep-alive.' To better suit intricate SPA application needs, the choice was made to permit the creation of multiple independent instances. It's crucial to remember to manually invoke the `artalk.destroy` method when releasing components to avoid memory leaks (Issue #660).
1.9 KiB
1.9 KiB
插件开发
使用 Artalk.use 来扩展 Artalk。
import Artalk from 'artalk'
Artalk.use((ctx) => {
ctx.on('inited', () => {
ctx.get('editor').setContent("Hello World")
})
ctx.on('list-loaded', () => {
console.log('评论列表加载完毕')
})
})
const artalk = Artalk.init({ ... })
请注意:
Artalk.use必须在Artalk.init之前调用。- 请勿依赖插件的加载顺序,请监听事件来执行插件逻辑。
- 当所有的插件加载完毕后,会触发
inited事件。
在 use 函数中,你可以通过 ctx 来访问 Artalk 的上下文。
Context
Context 对象包含了 Artalk 的上下文信息。
| 成员 | 说明 |
|---|---|
ctx.$root |
Artalk 容器元素 |
ctx.conf |
Artalk 配置 |
ctx.fetch |
加载数据数据 |
ctx.reload |
重载评论列表 |
ctx.on |
添加事件监听 |
ctx.off |
解除事件监听 |
ctx.trigger |
触发事件 |
ctx.inject |
依赖注入 |
ctx.get |
获取依赖 |
::: warning Context API 目前仍不稳定,开发可能会有变动,升级请关注 CHANGELOG。 :::
Artalk 拥有完整的 TypeScript 类型定义,你可以通过编辑器的自动补全来查看 API。
示例插件
Artalk 有很多内置的插件,你可以参考它们的源码来开发自己的插件。
@ArtalkJS/Artalk - src/plugins
我们还提供了一些外置的插件,同样可以参考:
| 插件 | 说明 |
|---|---|
| @artalk/plugin-katex | LaTeX 公式插件 |
| @artalk/plugin-lightbox | 图片灯箱插件 |