@uni-helper/vite-plugin-uni-middleware

Use route middleware in uni-app with Vite.
2,824 11
Install
npm install @uni-helper/vite-plugin-uni-middleware
Latest Version:0.0.1
Vue:3
License:MIT
Last Updated:Oct 15, 2023
Links: GitHub  ·  npm
Maintainer: modyqyw

@uni-helper/vite-plugin-uni-middleware

WIP Use route middleware in uni-app with Vite.

English | 简体中文

Installation

pnpm i -D @uni-helper/vite-plugin-uni-middleware

Usage

// vite.config.ts
import { defineConfig } from "vite";
import Uni from "@dcloudio/vite-plugin-uni";
import UniMiddleware from "@uni-helper/vite-plugin-uni-middleware";
export default defineConfig({
  plugins: [Uni(), UniMiddleware()],
});

Define the middleware in src/middleware

// src/middleware/auth.ts
import { defineMiddleware } from "@uni-helper/vite-plugin-uni-middleware/runtime";
import { useStore } from "../store";

export default defineMiddleware((to, from) => {
  const store = useStore();
  if (!store.isLogin) {
    return "/pages/login/index";
  }
});

Add middleware config on global or page in pages.json

// pages.json
{
  ...
  "middleware": ["global"],
  "pages": [{
    "path": "pages/index/index",
    "middleware": ["auth"]
  }]
  ...
}

Configuration

see types.ts

Notes

If you used vite-plugin-uni-pages, you will create pages.d.ts to define type of middleware

declare module "@uni-helper/vite-plugin-uni-pages" {
  export interface PagesConfig {
    middleware: string[];
  }
}
export {};

If you added config for page, just only use route-block

<route>
{
  "middleware": ["auth"]
}
</route>

Related Packages

@uni-helper/unplugin-uni-sfc

将使用 TypeScript / Less 的 uni-app SFC(.vue / .nvue)在构建阶段降级为 JavaScri...

293 2
@uni-helper/vite-plugin-uni-components

适用于 uni-app (Vue3) 的 unplugin-vue-components,按需自动导入组件

82,851 44
@uni-helper/uni-use

uni-app (vue3) 组合式工具集

25,772 182

Version History

Version Released Vue License
0.0.1 >=3.0.0 MIT