Pug テンプレートを使う
ラッパーは通常の Vue コンポーネントなので、<template lang="pug"> でも HTML テンプレートと同じように 使えます。props、v-model、イベント、名前付きスロットはそのまま使えます。
セットアップ
Vue のシングルファイルコンポーネントのコンパイラは、pug パッケージをインストールすると Pug テンプレートを コンパイルします。
npm install -D pugほかに変えるところはありません。はじめにで説明している、index.html の Polaris runtime の スクリプトと、s-* タグ用の isCustomElement オプションはそのままにしてください。
例
<script setup lang="ts">
import { ref, useTemplateRef } from 'vue'
import { SBanner, SButton, SModal, SPage, SSection, SStack, SText, STextArea, STextField } from 'polaris-vue-elements'
const name = ref('')
const notes = ref('')
const modal = useTemplateRef<InstanceType<typeof SModal>>('modal')
</script>
<template lang="pug">
SPage(heading="Settings")
SSection(heading="Profile")
SStack(gap="base")
SBanner(tone="info")
SText Changes save when you click Save.
STextField(v-model="name" label="Name")
STextArea(v-model="notes" label="Notes" :rows="3")
SButton(command-for="demo-modal" command="--show") Open
SButton(@click="modal?.$el.showOverlay()") Open from code
SModal#demo-modal(ref="modal" heading="Demo modal")
SText Modal content.
template(#primary-action)
SButton(variant="primary" command-for="demo-modal" command="--hide") Close
</template>HTML から Pug へ
| HTML テンプレート | Pug |
|---|---|
<SButton variant="primary">Save</SButton> | SButton(variant="primary") Save |
<SModal id="demo-modal"> | SModal#demo-modal |
<STextArea :rows="3" /> | STextArea(:rows="3") |
<STextField v-model="name" /> | STextField(v-model="name") |
<SButton @click="save"> | SButton(@click="save") |
<template #primary-action> | template(#primary-action) |
既知の注意点もそのまま当てはまります。数値型の props は引き続きバインドが必要で (:rows="3")、SModal の primary-action スロットに置けるのは、引き続き variant="primary" のボタン 1 つだけです。
Pug テンプレートの lint
ESLint と eslint-plugin-vue だけでは <template lang="pug"> を lint できません。 eslint-plugin-pug-vue は、vue/* の rule を Pug テンプレートに対して実行し、 eslint --fix でインデント、属性ブロック、引用符を修正する rule を追加します。
npm install -D eslint eslint-plugin-vue vue-eslint-parser eslint-plugin-pug-vue// eslint.config.js
import pluginVue from 'eslint-plugin-vue'
import vuePug from 'eslint-plugin-pug-vue'
export default [
...pluginVue.configs['flat/recommended'],
...vuePug.configs.recommended,
]flat/recommended を使うと、vue/attribute-hyphenation rule が commandFor のような camelCase の属性名を、 HTML と同様に Pug でも報告します。command-for と書く(ラッパーはどちらの形式も受け付けます)か、 この rule を無効にしてください。rule、エディターの設定、oxlint との併用については eslint-plugin-pug-vue のドキュメントを参照してください。