Pug 템플릿 사용하기
래퍼는 일반 Vue 컴포넌트이므로 <template lang="pug">에서도 HTML 템플릿과 똑같이 동작합니다. props, v-model, 이벤트, 이름 있는 슬롯이 모두 그대로 적용됩니다.
설정
pug 패키지를 설치하면 Vue의 싱글 파일 컴포넌트 컴파일러가 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" 버튼 하나만 넣을 수 있습니다.
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 문서를 참고하세요.