You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Odoo 15 自定义OWL组件setup方法中useService调用act_window失效问题

问题原因

旧版AbstractFieldOwl属于Odoo从老部件体系向Owl迁移的兼容组件,没有内置Owl的服务注入上下文环境,因此无法直接在其setup方法中调用useService系列Hook。

正确修复方案

方案1:主动调用父类setup(最优先)

你代码里把super.setup(...arguments)的注释打开即可,父类的setup方法会初始化服务注入所需的上下文,之后useService就能正常获取到action服务:

setup() {
  super.setup(...arguments); // 取消这行的注释
  this.actionService = useService('action');
}

调用action的时候直接用服务即可,不需要再通过__owl__层级找父组件的方法:

onClick() {
  this.actionService.doAction({
    type: 'ir.actions.act_window',
    res_model: this.props.value.model,
    res_id: this.props.value.res_id,
    views: [[false, 'form']],
    target: 'current'
  })
}

方案2:兼容旧版写法

如果你的Odoo版本过旧,父类setup执行后依然无法获取服务,可以用官方提供的兼容属性获取服务,不需要手动遍历__owl__层级:

onClick() {
  this.env.services.action.doAction({
    type: 'ir.actions.act_window',
    res_model: this.props.value.model,
    res_id: this.props.value.res_id,
    views: [[false, 'form']],
    target: 'current'
  })
}

注意:你现在用的this.__owl__.parent.parentWidget.do_action属于硬编码的临时方案,一旦组件嵌套层级变化就会失效,不建议长期使用。

内容的提问来源于stack exchange,提问作者Angel García

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 16:45:04