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

如何在Odoo v16现有网页中嵌入OWL组件?

在Odoo 16现有页面嵌入OWL组件及封装为网站片段

一、在现有页面(如网站首页)嵌入Counter组件

1. 准备Counter组件代码

复用官方教程中的Counter组件结构,确保你的模块下有:

  • static/src/counter/counter.js:组件核心逻辑
  • static/src/counter/counter.xml:组件模板
  • (可选)static/src/counter/counter.scss:组件样式文件

2. 加载组件资源到网站前端

在模块的views/assets.xml中,将组件资源注入网站前端资源栈:

<odoo>
    <template id="assets_frontend" inherit_id="website.assets_frontend">
        <xpath expr="." position="inside">
            <script type="text/javascript" src="/your_module_name/static/src/counter/counter.js"/>
            <link rel="stylesheet" type="text/scss" href="/your_module_name/static/src/counter/counter.scss"/>
            <script type="text/javascript" src="/your_module_name/static/src/counter/mount_counter.js"/>
        </xpath>
    </template>
</odoo>

替换your_module_name为你的实际模块名称。

3. 在目标页面添加组件挂载节点

以网站首页为例,在views/templates.xml中继承首页模板,插入组件挂载容器:

<odoo>
    <template id="homepage_add_counter" inherit_id="website.homepage">
        <xpath expr="//div[hasclass('o_homepage')]" position="inside">
            <div id="counter-root" class="counter-container"/>
        </xpath>
    </template>
</odoo>

可根据需求调整xpath定位到页面的具体位置。

4. 编写组件挂载逻辑

创建static/src/counter/mount_counter.js,实现页面加载完成后自动挂载组件:

import { mount } from "@odoo/owl";
import Counter from "./counter";

document.addEventListener('DOMContentLoaded', () => {
    const rootElement = document.getElementById('counter-root');
    if (rootElement) {
        mount(Counter, rootElement);
    }
});

5. 验证效果

升级模块后刷新网站首页,即可看到Counter组件正常渲染并交互。


二、封装Counter为网站片段

1. 定义片段结构与编辑器配置

在模块的views/snippets.xml中编写片段模板及编辑器逻辑:

<odoo>
    <!-- 片段基础结构 -->
    <template id="snippet_counter" name="Counter Component">
        <div class="oe_snippet" data-snippet="snippet_counter">
            <div class="counter-container"/>
        </div>
    </template>

    <!-- 片段编辑器挂载逻辑 -->
    <template id="snippet_counter_options" inherit_id="website.snippets.options">
        <xpath expr="." position="inside">
            <script type="text/javascript">
                odoo.define('your_module_name.snippet_counter', function (require) {
                    'use strict';
                    const options = require('website.snippets.options');
                    const { mount } = require('@odoo/owl');
                    const Counter = require('your_module_name.counter').default;

                    options.registry.CounterSnippet = options.Class.extend({
                        onBuilt: function () {
                            this._super.apply(this, arguments);
                            const root = this.$target.find('.counter-container')[0];
                            // 为每个片段实例生成唯一ID,避免挂载冲突
                            root.id = `counter-snippet-${Date.now()}`;
                            mount(Counter, root);
                        },
                    });
                    return { CounterSnippet: options.registry.CounterSnippet };
                });
            </script>
        </xpath>
    </template>
</odoo>

2. 注册片段资源

确保__manifest__.py的data字段包含该snippets.xml文件:

'data': [
    # 其他资源文件...
    'views/snippets.xml',
],

3. 测试片段功能

进入网站编辑器,在左侧片段面板中找到「Counter Component」,拖拽到页面任意位置,即可看到可交互的计数器片段。


内容的提问来源于stack exchange,提问作者M.A. Heshmat Khah

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 05:53:27