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>
了解 Oiyo 如何扫描和自动导入 API。

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>
前往了解 Vue 更多 API 信息

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>
前往了解 UniApp 更多 API 信息

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");
查看 createHttp 与 http 的完整能力

与 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 均采用自动导入。