---
url: /reference/interface/bridge/view.md
---

# view

`view` 视图对象指向当前加载资源的上下文。

导入：

```typescript
import { view } from "@pc-nexus/bridge";
```

内置方法：

## getContext

`getContext` 方法可以让你在扩展应用中获取上下文信息。

### **函数签名**

```typescript
function getContext(): Promise<NexusAppContext>;

export interface NexusAppContext<T = ExtensionData> {
    app: {
        id: string;
        version: string;
        licensing: { enabled: boolean };
    };
    environment: {
        id: string;
        type: EnvironmentType;
    };
    team: {
        id: string;
        url: string;
        locale: SupportedLocaleCode;
        timezone: string;
    };
    installation: {
        id: string;
    };
    user: {
        id: string;
        locale: SupportedLocaleCode;
        timezone: string;
    };
    extension: {
        key: string;
        local_id: string;
        target: string;
        location: string;
        data: T;
    };
}

export type ExtensionData = Record<string, any>;

export enum EnvironmentType {
    Development = "development",
    Production = "production",
    Staging = "staging",
}

export type SupportedLocaleCode = "zh-CN" | "en-US";
```

### 参数

空

### **返回值**

详细数据解释请参考： [上下文数据](/reference/resource/context)

### **示例**

```typescript
import { view } from "@pc-nexus/bridge";

const context = await view.getContext();
```

## setWindowTitle

`setWindowTitle`  方法允许更改当前 `document` 的标题，该方法只支持部分扩展模块。

### **函数签名**

```typescript
function setWindowTitle(newTitle: string): Promise<void>;
```

### 参数

### 返回值

空

### **示例**

```typescript
import { view } from "@pc-nexus/bridge";

await view.setWindowTitle("New Title");
```

## refresh

`refresh` 方法允许你再次刷新父页面的数据，而无需进行整个页面加载。该方法只支持部分扩展模块。

### **函数签名**

```typescript
function refresh(): Promise<void>;
```

### 参数

空

### 返回值

空

### **示例**

```typescript
import { view } from "@pc-nexus/bridge";

view.refresh();
```

## close

`close` 方法允许你关闭当前视图。例如: 关闭一个 dialog 模态框或者动态菜单打开的视图。

### **函数签名**

```typescript
function close(payload?: unknown): Promise<void>;
```

### 参数

### 返回值

空

### **示例**

```typescript
import { view } from "@pc-nexus/bridge";

view.close({ formValue: '' });
```

## onClose

`onClose` 方法允许你注册一个回调，当 Dialog（或其他视图）关闭时执行该回调函数。

### **函数签名**

```typescript
function onClose(callback: () => Promise<void>): Promise<void>;
```

### 参数

### 返回值

空

### **示例**

```typescript
import { view } from "@pc-nexus/bridge";

view.onClose(async () => {
  // do somethings
});
```

## isDialog

`isDialog` 方法用于判断当前资源是否在 Dialog 中运行。

### **函数签名**

```typescript
function isDialog(): Promise<boolean>;
```

### 参数

空

### 返回值

返回 `Promise<boolean>` 表示是否是一个 Dialog。

### **示例**

```typescript
import { view } from "@pc-nexus/bridge";

const isDialog = await view.isDialog();
```

## createHistory

`createHistory`  方法使你的应用能够操作当前页面 URL，以便在全页面应用内进行路由，使用时 `to` 属性始终相对于应用的 URL。该方法只支持部分扩展模块。

### **函数签名**

```typescript
function createHistory(): Promise<NexusHistory>;

export interface NexusHistory {
    action: HistoryAction;
    location: HistoryLocation;
    push(to: HistoryTo, state?: HistoryState): void;
    replace(to: HistoryTo, state?: HistoryState): void;
    go(delta: number): void;
    back(): void;
    forward(): void;
    listen(listener: HistoryListener): Promise<UnlistenCallback>;
}


export enum HistoryAction {
    Pop = "POP",
    Push = "PUSH",
    Replace = "REPLACE",
}

export type HistoryTo = string | Partial<HistoryPath>;

export type HistoryState = unknown;

export interface HistoryLocation extends HistoryPath {
    state: HistoryState;
}

export interface HistoryUpdate {
    action: HistoryAction;
    location: HistoryLocation;
}

export interface HistoryListener {
    (update: HistoryUpdate): void;
}

export type UnlistenCallback = () => void;

interface HistoryPath {
    pathname: string;
    search: string;
    hash: string;
}

```

### 参数

空

### 返回值

`Promise<NexusHistory>` 是一个 History 对象引用。

### **示例**

```typescript
import { view } from "@pc-nexus/bridge";

const history = await view.createHistory();

// e.g. URL begins as http://example.pingcode.com/apps/abc/123

const to: HistoryTo = '/page-1';
history.push(to);
// this updates the URL to http://example.pingcode.com/apps/abc/123/page-1


const to: HistoryTo = {
  pathname: '/page-2',
  search: '?fruit=apple&color=blue',
  hash: '#section',
};
const state = { from: 'pushLocation' };
history.push(to, state);
// this updates the URL to http://example.pingcode.com/apps/abc/123/page-2?fruit=apple&color=blue#section

history.go(-2);
// this updates the URL to http://example.pingcode.com/apps/abc/123
```

## submit

`submit` 方法允许你在上下文配置视图上提交表单。该方法只支持部分扩展模块。

### **函数签名**

```typescript
function submit<T = any>(payload?: T): Promise<boolean>;
```

### 参数

### 返回值

返回 `Promise<boolean>` 表示是否允许提交。

### **示例**

```typescript
import { view } from "@pc-nexus/bridge";

view.submit({ config: { name: 'lily' } });
```

## onSubmit

`onSubmit` 方法允许你注册一个回调函数。当宿主提交当前视图时，会执行该回调，并通过回调的返回值获取配置数据。该方法只支持部分扩展模块。

### 函数签名

```typescript
function onSubmit<TResult = unknown>(callback: ViewOnSubmitFn<TResult>): Promise<void>;

export type ViewOnSubmitFn<TResult = unknown> = () => TResult | Promise<TResult>;
```

### 参数

### 返回值

空

### 示例

```typescript
import { view } from "@pc-nexus/bridge";

await view.onSubmit(async () => {
  return {
    name: "lily",
    comment: "hello",
  };
});
```

## emitReadyEvent

`emitReadyEvent`  这个函数会通知 Wiki 页面当前 Block 插件已成功加载，可以进行导出或下一步任务。这样， Wiki 客户端（如我们的 PDF 导出服务）就能准确判断 Block 插件是否已完全加载，而无需依赖 DOM 扫描或时间判断。

### **函数签名**

```typescript
function emitReadyEvent(): Promise<void>;
```

### 参数

空

### 返回值

空

### **示例**

```typescript
import { view } from "@pc-nexus/bridge";

await view.emitReadyEvent();
```
