Vue / UniApp
vue、uni-app 与 oiyo 的运行时 API 由统一入口提供
oiyo/runtime 是 Oiyo 应用的运行时 API 入口,用于统一使用 Vue、UniApp 以及 Oiyo 自身提供的公开 API。
Oiyo 不改变 Vue 与 UniApp API 的语义,这些 API 仍按其所属框架的行为工作。
使用方式
你可以显式从 oiyo/runtime 导入:
<script setup lang="ts">
import { computed, onLaunch, ref } from "oiyo/runtime";
import type { Ref } from "oiyo/runtime";
const title: Ref<string> = ref("Hello Oiyo");
const upperTitle = computed(() => title.value.toUpperCase());
onLaunch(() => {
console.log("App Launch");
});
</script>
也可以在应用文件中 无需导入 的直接使用这些 API:
<script setup lang="ts">
const title: Ref<string> = ref("Hello Oiyo");
const upperTitle = computed(() => title.value.toUpperCase());
onLaunch(() => {
console.log("App Launch");
});
</script>
Vue API
Runtime 入口收敛 Vue 的常用组合式 API 和类型。常见使用包括:
| 分类 | 常见 API | 说明 |
|---|---|---|
| 响应式 | ref、reactive、readonly、computed | 创建和派生响应式状态。 |
| 响应式工具 | unref、isRef、toRef、toRefs | 处理响应式值和对象字段。 |
| 监听 | watch、watchEffect | 监听响应式数据变化。 |
| 调度 | nextTick | 等待下一次 DOM 或视图更新。 |
| 类型 | Ref、ComputedRef | 标注响应式状态和计算状态。 |
<script setup lang="ts">
import { computed, ref, watch } from "oiyo/runtime";
import type { Ref } from "oiyo/runtime";
const count: Ref<number> = ref(0);
const double = computed(() => count.value * 2);
watch(count, (value) => {
console.log("count changed:", value);
});
</script>
UniApp API
Runtime 入口也收敛 UniApp 暴露的生命周期与组合式 API。常见使用包括:
| 分类 | 常见 API | 说明 |
|---|---|---|
| 应用生命周期 | onLaunch、onShow、onHide | 在 App.vue 中处理应用启动、显示和隐藏。 |
| 页面生命周期 | onLoad、onReady、onUnload | 在页面文件中处理页面加载、就绪和卸载。 |
| 页面交互 | onPullDownRefresh、onReachBottom、onPageScroll | 处理下拉刷新、触底和页面滚动。 |
| 分享相关 | onShareAppMessage、onShareTimeline | 处理平台支持的分享能力。 |
<script setup>
import { onLoad, onPullDownRefresh, onReachBottom } from "oiyo/runtime";
onLoad((query) => {
console.log("page query:", query);
});
onPullDownRefresh(() => {
console.log("refresh");
});
onReachBottom(() => {
console.log("reach bottom");
});
</script>
Oiyo API
oiyo/runtime 同时导出 Oiyo 自身的运行时能力:
| 分类 | API | 说明 |
|---|---|---|
| 页面与根上下文 | definePageMeta、defineRootContext、useRootContext | 页面元信息、根上下文定义与读取。 |
| 占位组件 | OiyoPage、OiyoLayout | 页面插槽与布局渲染入口。 |
| 请求 | http、createHttp、createHttpAborter、UniAppAdapter、HttpError、isHttpError | 请求、上传、下载与中断。 |
| 路由中间件 | defineRouteMiddleware、goTo、abortNavigation | 页面导航守卫。 |
| 类型 | PageMeta、OiyoRootContext、HttpConfig、RouteMiddleware 等 | 运行时公开类型。 |
const api = createHttp({
baseURL: "https://api.example.com",
});
const data = await api.request("/pet/1");
与 uni 全局对象的关系
oiyo/runtime 不替代 uni 全局对象。
调用平台能力时,仍然直接使用 uni.*:
const { hostTheme } = uni.getSystemInfoSync();
uni.navigateTo({
url: "/pages/home/index",
});
使用建议
- 使用
oiyo/runtime作为运行时 API 的统一来源。 - Runtime API 的具体行为以 Vue 和 UniApp 对应 API 的官方语义为准。
- 在团队约定允许下,Runtime API 均采用自动导入。