Skip to content

Commit

Permalink
docs: beforePageLeave 支持浏览器 beforeunload 事件
Browse files Browse the repository at this point in the history
  • Loading branch information
bhuh12 committed Jul 23, 2020
1 parent 5d57e60 commit cb2b424
Show file tree
Hide file tree
Showing 22 changed files with 122 additions and 131 deletions.
14 changes: 11 additions & 3 deletions docs/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -361,10 +361,18 @@ RouterAlive 组件就绪
页面组件选项,与 `data`, `methods` 等选项并列配置

- 参数:
- `{Function} resolve` 执行后允许离开页签
- `{Function} reject` 执行后阻止离开页签

- `{Object} tab` 页签信息
- `{String} type` 离开类型: `close`: '关闭', `refresh`: '刷新', `replace`: '替换'
- `{String} type` 离开类型:
- `close`: 页签关闭
- `refresh`: 页签刷新
- `replace`: 页签被替换
- `leave`: 路由离开
- `unload`: 浏览器刷新或者关闭

- 返回值类型:
- `{String}` 离开类型为 `unload` 时,浏览器离开提示消息
- `{Promise}` 其他类型,`resolve` 离开,`reject` 阻止离开

### vm.\$routerTab

Expand Down
2 changes: 1 addition & 1 deletion docs/guide/advanced/dynamic-tab-info.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

RouterTab will listen to the component `this.routeTab` to dynamically update the tab info. You can change the title, icon, and tips of the tab by setting `this.routeTab`.

<doc-links demo="/default/tab-dynamic"></doc-links>
<doc-links demo="/default/tab-dynamic" />

### Computed (recommend)

Expand Down
2 changes: 1 addition & 1 deletion docs/guide/advanced/initial-tabs.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

You can configure your RouterTab to open default tabs. You do this by using the `tabs` property.

<doc-links api="#tabs" demo="/initial-tabs/"></doc-links>
<doc-links api="#tabs" demo="/initial-tabs/" />

**Example:**

Expand Down
68 changes: 37 additions & 31 deletions docs/guide/advanced/page-leave.md
Original file line number Diff line number Diff line change
@@ -1,54 +1,60 @@
# Leave Confirm

`BeforePageLeave` is triggered when the tab is **closed**, **refreshed**, or **replaced**, the Promise resolve and reject are used to allow or prevent the tab page from leaving.
When the tab **close**, **refresh**, **replace**, **leave** the current route, or the browser window **close**, **refresh**, `beforePageLeave` will be triggered, allow or prevent the tab page to leave by returning a `Promise`.

::: warning
`beforePageLeave` is at the outermost level of the component, not in `methods`
:::

- `beforePageLeave` is at the outermost level of the component, not in`methods`

- If you also need to block before the browser page closes or refreshes, please use
[`onbeforeunload`](https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload)
:::

<doc-links demo="/initial-tabs/page-leave"></doc-links>
<doc-links api="#beforepageleave" demo="/initial-tabs/page-leave" />

**Example:**

```javascript {3,15,21,23,28,29}
```javascript {13,15,18,23,33,44}
export default {
// confirm before leaving
beforePageLeave(resolve, reject, tab, type) {
// types
/**
* confirm before leaving
* @param {Object} tab tab info
* @param {String} type leaving type
* close: tab close
* refresh: tab refresh
* replace: tab replace
* leave: leave the route
* unload: browser window close or refresh
* @returns {String|Promise}
*/
beforePageLeave(tab, type) {
//If the value has not changed, just leave the tab
if (this.editValue === this.value) return

// When the browser window is refreshed or closed, supported browsers will display a confirmation message
if (type === 'unload') {
return `Your changes on the tab "${tab.title}" have not been completed, Do you want to leave?`
}

// leaving type
const action = {
close: 'shut down',
close: 'close',
refresh: 'refresh',
replace: 'replace',
leave: 'leave'
}[type]

const msg = `Are you sure you want to ${action} tab “${tab.title}`
const msg = `Are you sure to ${action} the tab “${tab.title}?`

//If the value has not changed, just leave the tab
if (this.editValue === this.value) {
resolve()
return
}

// Confirm the prompt when the value changes
if (confirm(msg)) {
resolve()
} else {
reject('Refuse to leave the page')
}
// Promise resolve to allow the page to leave
return new Promise((resolve, reject) => {
if (confirm(msg)) {
resolve()
} else {
reject(`Refuse to ${action} the page`)
}
})

/*
// The confirm component of Element is used here
// You need to configure closeOnHashChange to false
// to avoid the route switching causing the confirmation box to close
this.$confirm(msg, 'prompt', { closeOnHashChange: false })
.then(resolve)
.catch(reject)
*/
// this.$confirm(msg, 'prompt', { closeOnHashChange: false })
}
}
```
2 changes: 1 addition & 1 deletion docs/guide/advanced/restore.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ You can `restore` tabs after refreshing the page or logging in from another comp

RouterTab uses sessionStorage to store the cache information of the tabs

<doc-links api="#restore" demo="/restore/"></doc-links>
<doc-links api="#restore" demo="/restore/" />

**Default mode**

Expand Down
2 changes: 1 addition & 1 deletion docs/guide/advanced/slot.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ You can customize your tabs using the available slot `default`. Inside the slot
| index | Number | tab index |
| close | Function | tab closing method |

<doc-links demo="/slot/"></doc-links>
<doc-links demo="/slot/" />

**Example:**

Expand Down
5 changes: 3 additions & 2 deletions docs/guide/advanced/transition.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ You can change the default page and tab transitions by adding the `tab-transitio
- If the scope of the CSS component (configured `scoped`), you need to add `>>>` / `/deep/` or `::v-deep` before the selectors

- The .router-tab-item sets the `transition` and `transform-origin` styles by default, you may need to override it to avoid affecting the custom transition effect
:::

<doc-links api="#tab-transition" demo="/transition/"></doc-links>
:::

<doc-links api="#tab-transition" demo="/transition/" />

**Example:**

Expand Down
4 changes: 2 additions & 2 deletions docs/guide/essentials/i18n.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

Property `i18n` of RouterTab takes custom method for internationalization.

<doc-links api="#i18n" demo="/i18n/"></doc-links>
<doc-links api="#i18n" demo="/i18n/" />

**Example:**

Expand Down Expand Up @@ -68,7 +68,7 @@ By specifying property `language` on RouterTab, you could set component's displa

RouterTab has `zh-CN` and `en` as built-in languages, and `zh-CN` is the default option.

<doc-links api="#language" demo="/lang-en/"></doc-links>
<doc-links api="#language" demo="/lang-en/" />

**Specify language**

Expand Down
4 changes: 2 additions & 2 deletions docs/guide/essentials/iframe.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ This feature requires RouterTabRoutes from RouterTab. See [Essentials - Route Co

## Iframe Tab Operation

<doc-links api="#routertab-openiframetab" demo="/default/"></doc-links>
<doc-links api="#routertab-openiframetab" demo="/default/" />

#### Open iframe Tab

Expand Down Expand Up @@ -39,7 +39,7 @@ Supported iframe tab events are listed below:

Note that url jumping within iframe will also trigger `iframe-loaded` event.

<doc-links api="#iframe-mounted" demo="/iframe/"></doc-links>
<doc-links api="#iframe-mounted" demo="/iframe/" />

**Example:**

Expand Down
6 changes: 3 additions & 3 deletions docs/guide/essentials/operate.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ this.$router.push('/page/1')

This method will reload the existing cached tab by default, which might be usefule if you intend to **force-new-open** a tab.

<doc-links api="#routertab-open" demo="/default/"></doc-links>
<doc-links api="#routertab-open" demo="/default/" />

**Force-new-open**

Expand All @@ -42,7 +42,7 @@ this.$routerTab.open('/page/2')

You can close a tab with [`routerTab.close`](../../api/README.md#routertab-close)

<doc-links api="#routertab-close" demo="/default/"></doc-links>
<doc-links api="#routertab-close" demo="/default/" />

**Current tab**

Expand Down Expand Up @@ -96,7 +96,7 @@ this.$routerTab.close({

You can refresh a tab with [`routerTab.refresh`](../../api/README.md#routertab-refresh)

<doc-links api="#routertab-refresh" demo="/default/"></doc-links>
<doc-links api="#routertab-refresh" demo="/default/" />

**Current tab**

Expand Down
20 changes: 0 additions & 20 deletions docs/guide/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,6 @@ yarn
yarn lib:build
```

<!--
### 直接下载 / CDN
[https://unpkg.com/vue-router-tab/dist/lib/vue-router-tab.umd.min.js](https://unpkg.com/vue-router-tab/dist/lib/vue-router-tab.umd.min.js)
[https://unpkg.com/vue-router-tab/dist/lib/vue-router-tab.css](https://unpkg.com/vue-router-tab/dist/lib/vue-router-tab.css)
[Unpkg.com](https://unpkg.com) 提供了基于 NPM 的 CDN 链接。上面的链接会一直指向在 NPM 发布的最新版本。你也可以像 `https://unpkg.com/vue-router-tab@0.1.8/dist/lib/vue-router-tab.umd.min.js` 这样指定 版本号 或者 Tag。
在 Vue 后面加载 `vue-router-tab`,它会自动安装的:
``` html
<style src="/path/to/vue-router-tab.css"></style>
<script src="/path/to/vue.js"></script>
<script src="/path/to/vue-router.js"></script>
<script src="/path/to/vue-router-tab.js"></script>
```
-->

### Polyfill

Vue Router Tab uses [**Vue CLI**](https://cli.vuejs.org) to build as library, so there's no polyfills.
Expand Down
14 changes: 11 additions & 3 deletions docs/zh/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -361,10 +361,18 @@ RouterAlive 组件就绪
页面组件选项,与 `data`, `methods` 等选项并列配置

- 参数:
- `{Function} resolve` 执行后允许离开页签
- `{Function} reject` 执行后阻止离开页签

- `{Object} tab` 页签信息
- `{String} type` 离开类型: `close`: '关闭', `refresh`: '刷新', `replace`: '替换'
- `{String} type` 离开类型:
- `close`: 页签关闭
- `refresh`: 页签刷新
- `replace`: 页签被替换
- `leave`: 路由离开
- `unload`: 浏览器刷新或者关闭

- 返回值类型:
- `{String}` 离开类型为 `unload` 时,浏览器离开提示消息
- `{Promise}` 其他类型,`resolve` 离开,`reject` 阻止离开

### vm.\$routerTab

Expand Down
2 changes: 1 addition & 1 deletion docs/zh/guide/advanced/dynamic-tab-info.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

RouterTab 会监听组件 `this.routeTab` 来动态更新页签信息。您可以通过设置 `this.routeTab` 来更改页签的标题、图标、提示。

<doc-links demo="/default/tab-dynamic"></doc-links>
<doc-links demo="/default/tab-dynamic" />

### 通过 computed 计算属性(推荐)

Expand Down
2 changes: 1 addition & 1 deletion docs/zh/guide/advanced/initial-tabs.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

通过配置 RouterTab 组件的 `tabs` 属性,可以设置进入页面时默认显示的页签。

<doc-links api="#tabs" demo="/initial-tabs/"></doc-links>
<doc-links api="#tabs" demo="/initial-tabs/" />

**示例:**

Expand Down
63 changes: 35 additions & 28 deletions docs/zh/guide/advanced/page-leave.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,37 @@
# 页面离开确认

当页签**关闭****刷新****替换**时会触发 `beforePageLeave`,通过 `Promise``resolve``reject` 来允许或者阻止页签页面的离开。
当页签**关闭****刷新****替换****离开**当前路由,或浏览器窗口**关闭****刷新**时,会触发 `beforePageLeave`,通过 `Promise` 来允许或者阻止页签页面的离开。

::: warning
`beforePageLeave` 在组件的最外层,不是放在 `methods`
:::

- `beforePageLeave` 在组件的最外层,不是放在 `methods`

- 如果还需要在浏览器页面关闭或刷新前阻止,请使用
[`onbeforeunload`](https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload)
:::

<doc-links demo="/initial-tabs/page-leave"></doc-links>
<doc-links api="#beforepageleave" demo="/initial-tabs/page-leave" />

**示例:**

```javascript {3,15,21,23,28}
```javascript {13,15,18,23,33,44}
export default {
// 页面离开前确认
beforePageLeave(resolve, reject, tab, type) {
/**
* 页面离开前确认
* @param {Object} tab 页签信息
* @param {String} type 离开类型:
* close: 页签关闭
* refresh: 页签刷新
* replace: 页签被替换
* leave: 路由离开
* unload: 浏览器刷新或者关闭
* @returns {String|Promise}
*/
beforePageLeave(tab, type) {
// 值未改变,则直接离开页签
if (this.editValue === this.value) return

// 浏览器窗口刷新或者关闭时,支持的浏览器会展示确认消息
if (type === 'unload') {
return `您在“${tab.title}”页签的更改尚未完成,是否要离开?`
}

// 离开类型
const action = {
close: '关闭',
Expand All @@ -28,26 +42,19 @@ export default {

const msg = `您确认要${action}页签“${tab.title}”吗?`

// 值未改变,则直接离开页签
if (this.editValue === this.value) {
resolve()
return
}

// 值改变则确认提示
if (confirm(msg)) {
resolve()
} else {
reject('拒绝了页面离开')
}
// 返回 promise,resolve 离开,reject 阻止离开
return new Promise((resolve, reject) => {
// 值改变则确认提示
if (confirm(msg)) {
resolve()
} else {
reject(`您拒绝了${action}页签`)
}
})

/*
// 此处使用了 Element 的 confirm 组件
// 需将 closeOnHashChange 配置为 false,以避免路由切换导致确认框关闭
this.$confirm(msg, '提示', { closeOnHashChange: false })
.then(resolve)
.catch(reject)
*/
// return this.$confirm(msg, '提示', { closeOnHashChange: false })
}
}
```
2 changes: 1 addition & 1 deletion docs/zh/guide/advanced/restore.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

RouterTab 通过 sessionStorage 来存储页签缓存信息

<doc-links api="#restore" demo="/restore/"></doc-links>
<doc-links api="#restore" demo="/restore/" />

**默认方式**

Expand Down
2 changes: 1 addition & 1 deletion docs/zh/guide/advanced/slot.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ RouterTab 支持通过以下插槽个性化页签组件:
| index | Number | 页签索引 |
| close | Function | 页签关闭方法 |

<doc-links demo="/slot/"></doc-links>
<doc-links demo="/slot/" />

**示例:**

Expand Down
Loading

0 comments on commit cb2b424

Please sign in to comment.