Skip to content

Commit

Permalink
docs: doc
Browse files Browse the repository at this point in the history
  • Loading branch information
JasKang committed Sep 7, 2022
1 parent ee8e9a1 commit ee2a1b6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 24 deletions.
42 changes: 26 additions & 16 deletions docs/api/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,6 @@ outline: deep
**示例:**

```ts
definePage({
setup() {
const count = ref(0)
// 返回值会暴露给模板
return {
count,
}
},
})
// or
defineComponent({
setup() {
const count = ref(0)
Expand All @@ -37,15 +27,15 @@ defineComponent({
<view>{{count}}</view>
```

请注意在模板中访问从 `setup` 返回的 `ref` 时,它会自动浅层解包,因此你无须再在模板中为它写 .value。
请注意在模板中访问从 `setup` 返回的 `ref` 时,它会自动浅层解包,因此你无须再在模板中为它写 `.value`

## App

与小程序 `App` 相关的 API

### createApp

创建一个应用实例
创建一个应用实例, `setup()` 对应 `onLaunch` 生命周期。

- **类型:**

Expand All @@ -56,16 +46,36 @@ defineComponent({
```ts
type AppOptions = {
plugins?: Plugin[]
setup: AppSetup
setup: (options: LaunchShowOption) => Bindings | void
}
```

- **详细信息:**

参数 `options` 是一个组件选项对象,setup
参数 `options` 是一个选项对象,包括以下属性:

- **plugins**

#### plugins 插件
插件数组,详情见[页面和组件插件](/guide/plugin)

#### setup
- **setup** :pushpin:

app 入口函数,对应 `onLaunch` 生命周期,参数与其一致。返回的数据将会被绑定到 `app` 实例

- **示例:**

```ts
createApp({
plugins: [],
setup(options) {
return {
globalData: {},
}
},
})
```

```ts
// pages/xxxx.js
const { globalData } = getApp()
```
12 changes: 4 additions & 8 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,24 @@ import { wrapLifetimeHooks } from './lifetimes'
import { registerPlugins, type Plugin } from './plugin'
import { SharePlugin } from './plugins'

export type AppSetup = (this: void) => Bindings | void

export type AppOptions = {
setup: AppSetup
setup: (options: WechatMiniprogram.App.LaunchShowOption) => Bindings | void
plugins?: Plugin[]
errorHandler?: (err: unknown, instance: Instance | null, info: string) => void
}

export function createApp(options: AppOptions) {
const { setup, plugins = [], errorHandler } = options
const { setup, plugins = [] } = options
registerPlugins([...plugins, SharePlugin])

const lifetimes = wrapLifetimeHooks(APP_LIFETIMES)
const core = createCore('App').initHooks()

return App({
[CORE_KEY]: core,
errorHandler,
...lifetimes,
onLaunch(options) {
onLaunch(...args) {
setCurrentInstance({ [CORE_KEY]: core } as unknown as Instance)
const bindings = setup() || {}
const bindings = setup(...args) || {}
unsetCurrentInstance()
for (const key of Object.keys(bindings)) {
this[key] = bindings[key]
Expand Down

1 comment on commit ee2a1b6

@vercel
Copy link

@vercel vercel bot commented on ee2a1b6 Sep 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

rubic – ./

rubic.vercel.app
rubic-git-main-jaskang.vercel.app
rubic-jaskang.vercel.app

Please sign in to comment.