> For AI agents: the complete documentation index is available at /next/zh/llms.txt, the full documentation bundle is available at /next/zh/llms-full.txt.

# `<frame>`

APISummary
一个类似 HTML `<iframe>` 的页面元素，可以将一个 Lynx 页面迁入当前页面中。

## 使用指南

以下为主页面通过 `<frame>` 元素加载 frame 内嵌页面的示例

- 主页面通过 `data`、`global-props` 属性向内嵌页面传递 `initData`、`globalProps` 数据
- 主页面通过 `bindload` 事件监听内嵌页面加载情况，也可以通过 `bindloadmetrics` 获取加载指标

### 主页面

**下面是一个示例: frame**

**Bundle:** `dist/frame.lynx.bundle`

```tsx {35-41}
// Copyright 2025 The Lynx Authors. All rights reserved.
// Licensed under the Apache License Version 2.0 that can be found in the
// LICENSE file in the root directory of this source tree.

import { type InitData, root, useState } from "@lynx-js/react";
import type { FrameLoadEvent, GlobalProps } from "@lynx-js/types";

const App = () => {
  const [bindLoadEvent, setBindLoadEvent] = useState({
    url: "",
    statusCode: 0,
    statusMessage: "",
  });
  const [initData, setInitData] = useState<InitData>({
    id: 123,
    name: "Alice",
    showDetail: true,
  });
  const [globalProps, setGlobalProps] = useState<GlobalProps>({
    message: "hello globalProps!",
    status: 42,
    loaded: true,
  });
  const handleLoad = (e: FrameLoadEvent) => {
    setBindLoadEvent(e.detail);
  };

  const frameUrl = `${process.env.ASSET_PREFIX}/in_frame.lynx.bundle`;
  return (
    <view>
      <text style={{ fontSize: "20px", color: "black", padding: "10px" }}>
        Below is the inline text example in frame page.
      </text>

      <frame
        style={{ width: "100%", height: "150px", border: "3px solid red" }}
        src={frameUrl}
        data={initData}
        bindload={handleLoad}
        global-props={globalProps}
      />
      <view
        style={{
          padding: "10px",
          width: "100%",
          height: "100px",
          border: "3px solid red",
        }}
        bindtap={() => setInitData({ ...initData, showDetail: !initData.showDetail })}
      >
        <text style={{ color: "black", fontSize: "20px" }}>
          {initData.showDetail ? "Hide Detail" : "Show Detail"}
        </text>
      </view>
      <text>load url: {bindLoadEvent.url}</text>
      <text>status code: {bindLoadEvent.statusCode}</text>
      <text>status message: {bindLoadEvent.statusMessage}</text>
    </view>
  );
};

root.render(<App />);

```

v0.2.0



### frame 内嵌页面

**下面是一个示例: frame**

**Bundle:** `dist/frame.lynx.bundle`

```tsx
// Copyright 2025 The Lynx Authors. All rights reserved.
// Licensed under the Apache License Version 2.0 that can be found in the
// LICENSE file in the root directory of this source tree.

import { root, useInitData } from "@lynx-js/react";

export function App() {
  const initData = useInitData();
  const globalProps = lynx.__globalProps;

  return (
    <view>
      <text style={{ fontSize: "30px", color: "orange" }}>I'm in frame!</text>
      <view style={{ border: "3px solid blue" }}>
        <text>{initData.id}</text>
        <text>{initData.name}</text>
        <text>{initData.showDetail ? "true" : "false"}</text>
        <text>{globalProps.message}</text>
        <text>{globalProps.status}</text>
        <text>{globalProps.loaded ? "true" : "false"}</text>
      </view>
    </view>
  );
}

root.render(<App />);

```

v0.2.0



:::tip
`<frame>` 元件于 3.4 引入，需要使用该元件的场景，需将依赖的 Lynx 版本升级至 3.4 及以上。
:::

:::tip
从 3.8 开始，可以通过 `auto-width` 和 `auto-height` 让 `<frame>` 跟随内嵌页面内容尺寸。未开启自动尺寸或低于该版本时，仍需要手动设置 frame 的宽高。首次内容尺寸未初始化前，可在 3.9 及以上通过 `preset-width` 和 `preset-height` 设置预设尺寸。
:::

## 属性

### `auto-height`

Android
iOS
3.8

```tsx
// @默认值: false
'auto-height'?: boolean;
```

使框架高度跟随嵌入的 Lynx 页面的内容高度。启用此功能后，嵌入的页面可以报告其内容大小，框架会将该值用作其测量高度。

### `auto-width`

Android
iOS
3.8

```tsx
// @默认值: false
'auto-width'?: boolean;
```

使框架宽度跟随嵌入的 Lynx 页面的内容宽度。启用此功能后，嵌入的页面可以报告其内容大小，框架会将该值用作其测量宽度。

### `data`

Android
iOS
3.4

```tsx
data?: Record<string, unknown>;
```

将数据传递给框架内嵌套的 Lynx 页面。

### `enable-multi-async-thread`

Android
iOS
3.9

```tsx
// @默认值: false
'enable-multi-async-thread'?: boolean;
```

覆盖内嵌 Lynx 页面是否使用多个异步线程。未设置时，frame 会继承宿主页面的配置。

### `global-props`

Android
iOS
3.6

```tsx
'global-props'?: Record<string, unknown>;
```

将
`globalProps`
传递给框架中嵌入的 Lynx 页面。嵌入的页面可以通过
`lynx.__globalProps`
读取它。

### `preset-height`

Android
iOS
3.9

```tsx
'preset-height'?: `${number}px` | `${number}rpx`;
```

设置内嵌 Lynx 页面收到初始化内容区域前使用的预设高度。

### `preset-width`

Android
iOS
3.9

```tsx
'preset-width'?: `${number}px` | `${number}rpx`;
```

设置内嵌 Lynx 页面收到初始化内容区域前使用的预设宽度。

### `src`

Android
iOS
3.4

```tsx
src: string;
```

设置框架资源的加载路径。

## 事件

前端可以绑定相应的事件回调来监听元素的运行时行为，如下所示。

### `bindload`

Android
iOS
3.6

```tsx
bindload = (e: FrameLoadEvent) => {};
```

| 字段 | 类型 | 是否可选 | 默认值 | 平台 | 版本 | 描述 |
| --- | --- | --- | --- | --- | --- | --- |
| statusCode | 数字 | 否 | – | Android iOS 3.6 | 3.6 | 框架加载状态码。 |
| statusMessage | 字符串 | 否 | – | Android iOS 3.6 | 3.6 | 框架加载状态消息。 |
| url | 字符串 | 否 | – | Android iOS 3.6 | 3.6 | 框架加载的 URL。 |

绑定框架加载事件回调。

### `bindloadmetrics`

Android
iOS
3.9

```tsx
bindloadmetrics = (e: FrameLoadMetricsEvent) => {};
```

| 字段 | 类型 | 是否可选 | 默认值 | 平台 | 版本 | 描述 |
| --- | --- | --- | --- | --- | --- | --- |
| entry | FrameLoadMetricsEntry | 否 | – | Android iOS 3.9 | 3.9 | 内嵌 Lynx 页面发出的原始 `loadBundle` 性能条目。 |
| mode | 字符串 | 否 | – | Android iOS 3.9 | 3.9 | frame 的加载模式。 |
| url | 字符串 | 否 | – | Android iOS 3.9 | 3.9 | frame 加载的 URL。 |

绑定 frame 加载指标事件回调。

## 兼容性

**Compatibility Table**

**Query:** `elements.frame`

[Source](https://github.com/lynx-family/lynx-website/tree/main/packages/lynx-compat-data/elements/frame.json)

| Feature | Android | HarmonyOS | iOS | macOS | Windows | Web |
| --- | --- | --- | --- | --- | --- | --- |
| frame | Yes (3.4) | No | Yes (3.4) | No | No | Yes |
| src | Yes (3.4) | No | Yes (3.4) | No | No | Yes |
| data | Yes (3.4) | No | Yes (3.4) | No | No | Yes |
| global-props | Yes (3.6) | No | Yes (3.5) | No | No | Yes |
| auto-width | Yes (3.8) | No | Yes (3.8) | No | No | No |
| auto-height | Yes (3.8) | No | Yes (3.8) | No | No | Yes |
| preset-width | Yes (4.0) | No | Yes (3.7) | No | No | No |
| preset-height | Yes (4.0) | No | Yes (3.7) | No | No | No |
| enable-multi-async-thread | Yes (3.9) | No | Yes (3.9) | No | No | No |
| bindload | Yes (3.6) | No | Yes (3.5) | No | No | Yes |
| bindloadmetrics | Yes (3.9) | No | Yes (3.9) | No | No | No |
| boundingClientRect | Yes (3.4) | No | Yes (3.4) | No | No | Yes |
| requestAccessibilityFocus | Yes (3.4) | No | Yes (3.4) | No | No | No |
| scrollIntoView | Yes (3.4) | No | Yes (3.4) | No | No | Yes |
| setFocus | No | No | No | No | No | No |
| takeScreenshot | Yes (3.4) | No | Yes (3.4) | No | No | No |

**`frame`**

frame

**`boundingClientRect`**

<code>boundingClientRect</code>

**`requestAccessibilityFocus`**

<code>requestAccessibilityFocus</code>

**`scrollIntoView`**

<code>scrollIntoView</code>

**`setFocus`**

<code>setFocus</code>

**`takeScreenshot`**

<code>takeScreenshot</code>

