vize:croquis/cf/duplicate-id
同じ要素 id が複数のコンポーネントで使われています。
既定の重大度: warning
適用範囲: 解析対象のコンポーネント構成と、下記に示す対応済みの情報
自動修正: なし。関連ファイルを確認して修正してください
オプション: コード固有のオプションはありません。対応済みの CLI 検出は重大度を変更できます
import { defineConfig } from "@vizejs/vite-plugin/vite-plus";
export default defineConfig({
lint: {
vize: { preset: "incremental", crossFile: true,
rules: { "croquis/cf/duplicate-id": "warn" },
},
},
});
vp run lint
共通のプロジェクト ファイル
以下のファイルは悪い例・良い例で共通です。import する Vue と、例で使う場合は vue-router / Pinia をインストールしてください。バージョン固有の注意がある場合は、その前提に合わせてください。エントリー ファイルでコンポーネントの関係を明確にしています。
main.ts
import { createApp } from "vue";
import Root from "./CheckoutForm.vue";
createApp(Root).mount("#app");
index.html
<div id="app"></div>
<script type="module" src="/main.ts"></script>
悪い
描画する shipping と billing の両方が id="postal-code" を使い、label の文書内の参照先が重複します。
CheckoutForm.vue
<script setup lang="ts">
import BillingAddress from "./BillingAddress.vue";
import ShippingAddress from "./ShippingAddress.vue";
</script>
<template>
<ShippingAddress />
<BillingAddress />
</template>
ShippingAddress.vue
<template>
<label for="postal-code">Shipping postal code</label>
<input id="postal-code" />
</template>
BillingAddress.vue
<template>
<label for="postal-code">Billing postal code</label>
<input id="postal-code" />
</template>
良い
各コンポーネントの useId() を label と input の両方に binding し、固定 ID の重複をなくします。
CheckoutForm.vue
<script setup lang="ts">
import BillingAddress from "./BillingAddress.vue";
import ShippingAddress from "./ShippingAddress.vue";
</script>
<template>
<ShippingAddress />
<BillingAddress />
</template>
ShippingAddress.vue
<script setup lang="ts">
import { useId } from "vue";
const postalCodeId = useId();
</script>
<template>
<label :for="postalCodeId">Shipping postal code</label>
<input :id="postalCodeId" />
</template>
BillingAddress.vue
<script setup lang="ts">
import { useId } from "vue";
const postalCodeId = useId();
</script>
<template>
<label :for="postalCodeId">Billing postal code</label>
<input :id="postalCodeId" />
</template>
良い例のファイルは上で説明した変更を示します。プロジェクトには別の検出が残る場合があります。