在主题中使用 <script type='text/discourse-plugin'> 或 <script type='text/x-handlebars'> 现已弃用。主题中所有使用这些标签的地方应根据以下说明进行更新。
常规的 <script> 和 <script type='text/javascript'> 不受此更改影响。
时间表
这些是粗略估计,可能会有变动
-
2025 年 5 月 - 启用控制台弃用警告消息
-
2025 年 7 月 - 启用管理员警告横幅
-
2025 年 9 月下旬2026 年 3 月 - 功能移除
转换 <script type='text/x-handlebars'>
使用此方法引入的模板应移至专用的 .hbs 文件,或重构为 .gjs 文件。
如需保留为 HBS,连接器模板可以放置在:
{theme}/javascripts/discourse/connectors/{outlet-name}/{connector-name}.hbs
组件模板可以放置在:
{theme}/javascripts/discourse/components/{component-name}.hbs
自 2026 年 3 月起,
.hbs文件也已弃用。完成此转换后,请参阅 Deprecating .hbs file extension in themes and plugins 中的说明。
要以现代的 .gjs 格式构建连接器和组件,请查看主题开发者教程的此章节:
转换 <script type='text/discourse-plugin'>
这些标签中的代码可以迁移到专用的 JavaScript 文件中。
如果您通过管理面板界面开发主题,请将代码从 <script> 中复制出来,并移至 JS 选项卡(显示 // your code here 的位置)。
如果您在本地开发主题,请在以下位置创建一个新文件:
{theme}/javascripts/discourse/api-initializers/init-theme.js
然后添加此包装器,并将您的代码放在指定位置:
import { apiInitializer } from "discourse/lib/api";
export default apiInitializer((api) => {
// 您的代码
});
在 script 标签中,导入其他 JS 模块的唯一方法是使用 require() 语法。虽然这在 .js 文件中仍然有效,但它很快就会被弃用,所以现在是将其转换为现代 ES6 导入的好时机。例如:
- const I18n = require("discourse-i18n").default;
+ import I18n from "discourse-i18n";
- const { h } = require("virtual-dom");
+ import { h } from "virtual-dom";
有关 JS 初始化程序的更多信息:


