钩子

Owl hooks 是一种将代码进行因式分解的方式,即使它依赖于某个组件的生命周期。Owl 提供的大多数钩子都与组件的生命周期相关,但其中一些(例如 useComponent)提供了一种构建特定钩子的方法。

借助这些钩子,可以构建许多自定义钩子,帮助解决特定问题,或使一些常见任务变得更容易。本页其余部分记录了 Odoo web 框架提供的钩子列表。

名称

简介

useAssets

加载资源

useAutofocus

自动聚焦 autofocus 引用的元素

useBus

订阅和取消订阅总线

usePager

显示视图控制面板的分页器。

usePosition

将元素相对于目标进行定位

useAssets

位置

@web/core/assets

说明

详情请参阅 资源懒加载 一节。

useAutofocus

位置

@web/core/utils/hooks

说明

只要当前组件中由 t-ref=”autofocus” 引用的元素出现在 DOM 中且之前未显示过,就立即聚焦该元素。

import { useAutofocus } from "@web/core/utils/hooks";

class Comp {
  setup() {
    this.inputRef = useAutofocus();
  }
  static template = "Comp";
}
<t t-name="Comp" owl="1">
  <input t-ref="autofocus" type="text"/>
</t>

API

useAutofocus()
返回

the element reference.

useBus

位置

@web/core/utils/hooks

说明

向总线添加并清除事件监听器。此钩子可确保在组件卸载时正确清除监听器。

import { useBus } from "@web/core/utils/hooks";

class MyComponent {
  setup() {
    useBus(this.env.bus, "some-event", event => {
      console.log(event);
    });
  }
}

API

useBus(bus, eventName, callback)
参数
  • bus (EventBus()) – the target event bus

  • eventName (string()) – the name of the event that we want to listen to

  • callback (function()) – listener callback

usePager

位置

@web/search/pager_hook

说明

显示视图控制面板的 分页器。此钩子会正确设置 env.config,以便向分页器提供 props。

import { usePager } from "@web/search/pager_hook";

class CustomView {
  setup() {
    const state = owl.hooks.useState({
      offset: 0,
      limit: 80,
      total: 50,
    });
    usePager(() => {
      return {
        offset: this.state.offset,
        limit: this.state.limit,
        total: this.state.total,
        onUpdate: (newState) => {
          Object.assign(this.state, newState);
        },
      };
    });
  }
}

API

usePager(getPagerProps)
参数
  • getPagerProps (function()) – function that returns the pager props.

usePosition

位置

@web/core/position_hook

说明

帮助将一个 HTMLElement(popper)相对于另一个 HTMLElement(reference)进行定位。此钩子可确保在窗口调整大小/滚动时更新定位。

import { usePosition } from "@web/core/position_hook";

class MyPopover extends owl.Component {
  setup() {
    // Here, the reference is the target props, which is an HTMLElement
    usePosition(this.props.target);
  }
}
MyPopover.template = owl.tags.xml`
  <div t-ref="popper">
    I am positioned through a wonderful hook!
  </div>
`;

Important

应使用 t-ref 指令 来指定 popper 元素。

API

usePosition(reference[, options])
参数
  • reference (HTMLElement or ()=>HTMLElement()) – the target HTMLElement to be positioned from

  • options (Options()) – the positioning options (see table below)

选项

类型

说明

popper

string

这是将要被定位元素的 useRef 引用。默认值为 "popper"。

container

HTMLElement

popper 不应溢出的容器。如果发生溢出,会尝试其他 popper 位置,直到找到不会溢出的位置。(默认值:<html/> 节点)

margin

数字

popper 元素与 reference 元素之间额外增加的边距(默认值:0)

position

Direction[-Variant]

所需的位置。它是由一个 Direction 和一个 Variant 以短横线分隔组成的字符串。Direction 可以是:top、bottom、right、left。Variant 可以是:start、middle、end。Variant 可以省略(默认变体为 middle)。合法位置的示例:right-end、top-start、left-middle、left。(默认位置:bottom)

onPositioned

(el: HTMLElement, position: PositioningSolution) => void(无返回值)

每次发生定位时都会调用的回调函数(例如组件挂载/修补时、文档滚动时、窗口调整大小时……)。可用于例如根据当前位置进行动态样式设置。PositioningSolution 是一个具有以下类型的对象:{ direction: Direction, variant: Variant, top: number, left: number }。

Example

import { usePosition } from "@web/core/position_hook";

class DropMenu extends owl.Component {
  setup() {
    const toggler = owl.useRef("toggler");
    usePosition(
      () => toggler.el,
      {
        popper: "menu",
        position: "right-start",
        onPositioned: (el, { direction, variant }) => {
          el.classList.add(`dm-${direction}`); // -> "dm-top" "dm-right" "dm-bottom" "dm-left"
          el.style.backgroundColor = variant === "middle" ? "red" : "blue";
        },
      },
    );
  }
}
DropMenu.template = owl.tags.xml`
  <button t-ref="toggler">Toggle Menu</button>
  <div t-ref="menu">
    <t t-slot="default">
      This is the menu default content.
    </t>
  </div>
`;