如何修改Odoo 15页面标题?原修改代码无法生效
Odoo 15 修改页面标题解决方案
你的旧代码失效是因为Odoo 15 前端架构已从原Backbone框架切换到OWL框架,原有的openerp命名空间和WebClient.include方式不再适用。以下是两种可行的解决方法:
方法一:设置固定自定义标题
- 在你的模块下创建静态JS文件,比如
static/src/js/web_client.js,内容如下:
/** @odoo-module **/ import { WebClient } from '@web/webclient/webclient'; import { patch } from '@web/core/utils/patch'; patch(WebClient.prototype, 'your_module_name.WebClient', { setup() { this._super(...arguments); // 替换为你需要的标题文本 document.title = "你的自定义标题"; }, });
- 在模块的
__manifest__.py中添加该JS文件的资源引用:
{ # 保留模块原有配置 'assets': { 'web.assets_backend': [ 'your_module_name/static/src/js/web_client.js', ], }, }
方法二:根据路由动态修改标题
如果需要根据当前页面路由动态调整标题,可使用以下代码:
/** @odoo-module **/ import { WebClient } from '@web/webclient/webclient'; import { patch } from '@web/core/utils/patch'; import { useService } from '@web/core/utils/hooks'; patch(WebClient.prototype, 'your_module_name.WebClient', { setup() { this._super(...arguments); const router = useService('router'); // 监听路由变化,动态生成标题 router.beforeEach((to) => { document.title = to.name ? `${to.name} - 你的系统名称` : "你的自定义标题"; }); }, });
注意事项
- 替换代码中的
your_module_name为你的实际模块名称 - 配置完成后,升级模块并刷新页面,自定义标题即可生效
内容的提问来源于stack exchange,提问作者Getaw Dejen
相关产品推荐
相关产品推荐

