Derived Modular Arch
指南

Vue + Vite

Vue 3 上的 DMA:SFC、composables、provide/inject。

可运行的 Vue 3 + Vite 示例:组合根 src/app/、SFC 模块、就近放置的 composables、provide/inject 用于横切上下文。

源码:examples/vue-vite

组合根

src/app/
├── App.vue            # nav, badges, mount */public/*
├── AppProviders.vue   # provide(shopThemeKey, …)
└── app.css            # global reset + CSS variables

App.vue 通过 emit → 公共 API 连接 catalog 与 notifications——无 feature → feature

<CatalogPage @added-to-cart="onAddedToCart" />
import { addNotification } from "@/features/notifications/public/notifications-api";

Composables 规则

Composables 留在 feature 文件夹,直到第二个模块导入相同逻辑:

Composable位置原因
use-catalog-search.tsfeatures/catalog/仅 catalog
use-checkout.tsfeatures/checkout/仅 checkout
use-notifications.tsfeatures/notifications/仅 notifications

提升(promotion):产品状态 → services/,纯 helper → shared/lib

provide / inject

横切主题——在 app 中,而非 feature:

<!-- AppProviders.vue -->
<script setup>
import { provide } from "vue";
import { shopThemeKey } from "@/shared/domain/shop-theme";

provide(shopThemeKey, { accent: "teal" });
</script>
<!-- features/profile.vue -->
<script setup>
import { inject } from "vue";
import { shopThemeKey } from "@/shared/domain/shop-theme";

const theme = inject(shopThemeKey);
</script>

注入 key 放在 shared/domain/——可移植,无产品逻辑。

SFC 边界

文件
App.vueapp/——只挂载 */public/*
AppProviders.vueapp/——provide 上下文
catalog-page.vuefeature public/——相对路径 → composables
profile.vuestage-0——inject
cart.tsservice——与框架无关

public/ 通过相对路径导入内部(../use-catalog.ts)。跨模块——@/ 别名指向 */public/*

代码放哪里

任务文件夹
外壳、布局src/app/
产品屏幕features/<name>/public/<page>.vue
Feature composablefeature 内就近放置
2+ 消费者services/<name>/public/
UI 原语shared/ui/
注入 keyshared/domain/
类型shared/model/

样式

类型位置
全局基线app/app.css
外壳布局App.vue <style scoped>
Feature 页面public/*.vue scoped 或 CSS Modules
SCSS(可选)profile.vue <style lang="scss" scoped>
共享 UIshared/ui/*.vue scoped

CSS Modules:catalog-page.module.css + <style module src="…">

测试

就近放置的 *.test.ts(Vitest,environment: "node"):

  • use-catalog-search.test.ts
  • checkout.store.test.ts
  • services/cart/public/cart.test.ts

组件测试——可选,使用 @vue/test-utils

命令

bun run dev
bun run build
bun run dma-check   # dma check .
bun run lint        # biome + @derived-modular/biome-plugin
bun run test

下一步

On this page