钩子¶
Owl hooks 是一种将代码进行因式分解的方式,即使它依赖于某个组件的生命周期。Owl 提供的大多数钩子都与组件的生命周期相关,但其中一些(例如 useComponent)提供了一种构建特定钩子的方法。
借助这些钩子,可以构建许多自定义钩子,帮助解决特定问题,或使一些常见任务变得更容易。本页其余部分记录了 Odoo web 框架提供的钩子列表。
名称 |
简介 |
|---|---|
加载资源 |
|
自动聚焦 autofocus 引用的元素 |
|
订阅和取消订阅总线 |
|
显示视图控制面板的分页器。 |
|
将元素相对于目标进行定位 |
|
在 input 或 textarea 获得焦点时启用拼写检查 |
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">
<input t-ref="autofocus" type="text"/>
</t>
API¶
- useAutofocus()¶
- 返回
元素引用。
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()) – 目标事件总线eventName (
string()) – 要监听的事件的名称callback (
function()) – 监听器回调
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()) – 返回分页器 props 的函数。
usePosition¶
位置¶
@web/core/position_hook
说明¶
帮助将一个 HTMLElement(popper)相对于另一个 HTMLElement(reference)进行定位。此钩子可确保在窗口调整大小/滚动时更新定位。
import { usePosition } from "@web/core/position_hook";
import { Component, xml } from "@odoo/owl";
class MyPopover extends Component {
static template = xml`
<div t-ref="popper">
I am positioned through a wonderful hook!
</div>
`;
setup() {
// Here, the reference is the target props, which is an HTMLElement
usePosition(this.props.target);
}
}
Important
应使用 t-ref 指令 来指定 popper 元素。
API¶
- usePosition(reference[, options])¶
- 参数
reference (
HTMLElement or ()=>HTMLElement()) – 作为定位参照的目标 HTMLElementoptions (
Options()) – 定位选项(见下表)
选项 |
类型 |
说明 |
|---|---|---|
|
string |
这是将要被定位元素的 useRef 引用。默认值为 |
|
HTMLElement |
popper 不应溢出的容器。如果发生溢出,会尝试其他 popper 位置,直到找到不会溢出的位置。(默认值: |
|
number |
popper 元素与 reference 元素之间额外增加的边距(默认值: |
|
Direction[-Variant] |
期望的位置。它是一个由 |
|
(el: HTMLElement, position: PositioningSolution) => void |
每次发生定位时都会调用的回调函数(例如组件挂载/修补时、文档滚动时、窗口调整大小时……)。可用于例如根据当前位置进行动态样式设置。 |
Example
import { Component, xml, useRef } from "@odoo/owl";
import { usePosition } from "@web/core/position_hook";
class DropMenu extends Component {
static template = xml`
<button t-ref="toggler">Toggle Menu</button>
<div t-ref="menu">
<t t-slot="default">
This is the menu default content.
</t>
</div>
`;
setup() {
const toggler = 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";
},
},
);
}
}
useSpellCheck¶
位置¶
@web/core/utils/hooks
说明¶
当当前组件中带有 t-ref="spellcheck" 的 input 或 textarea 获得焦点时,为其激活拼写检查状态。该状态在失焦时会被移除,红色轮廓也随之消失,从而提高内容的可读性。
此钩子也可用于任何带有 contenteditable 属性的 HTML 元素。要在可能被该钩子启用的元素上完全禁用拼写检查,请在该元素上显式将 spellcheck 属性设为 false。
Example
在以下示例中,第一个 input、textarea 以及带有 contenteditable="true" 的 div 将启用拼写检查。
import { useSpellCheck } from "@web/core/utils/hooks";
class Comp {
setup() {
this.simpleRef = useSpellCheck();
this.customRef = useSpellCheck({ refName: "custom" });
this.nodeRef = useSpellCheck({ refName: "container" });
}
static template = "Comp";
}
<t t-name="Comp">
<input t-ref="spellcheck" type="text"/>
<textarea t-ref="custom"/>
<div t-ref="container">
<input type="text" spellcheck="false"/>
<div contenteditable="true"/>
</div>
</t>
API¶
- useSpellCheck([options])¶
- 参数
options (
Options()) – 拼写检查选项(见下表)
选项 |
类型 |
说明 |
|---|---|---|
|
string |
这是将要启用拼写检查的元素的 useRef 引用。 |