— prop, v-model, event và named slot trong Pug, và lint template bằng ESLint.">
Skip to content

Dùng template Pug ​

Các wrapper là component Vue bình thường, nên chúng hoạt động trong <template lang="pug"> giống như trong template HTML: prop, v-model, event và named slot đều dùng như cũ.

Cài đặt ​

Trình biên dịch single-file component của Vue biên dịch được template Pug khi đã cài package pug:

sh
npm install -D pug

Không có gì khác thay đổi: giữ script Polaris runtime trong index.html và tuỳ chọn isCustomElement cho thẻ s-* như trong Bắt đầu.

Ví dụ ​

vue
<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>

Từ HTML sang Pug ​

Trong template HTMLTrong 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)

Các điểm bất thường đã biết vẫn áp dụng nguyên vẹn: prop kiểu số vẫn phải được bind (:rows="3"), và slot primary-action của SModal vẫn chỉ nhận một button có variant="primary".

Lint template Pug ​

ESLint cùng eslint-plugin-vue tự nó không lint <template lang="pug">. eslint-plugin-pug-vue chạy các rule vue/* của bạn trên template Pug và thêm các rule sửa thụt lề, khối attribute và dấu nháy bằng eslint --fix:

sh
npm install -D eslint eslint-plugin-vue vue-eslint-parser eslint-plugin-pug-vue
js
// 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,
]

Với flat/recommended, rule vue/attribute-hyphenation báo lỗi các tên attribute camelCase như commandFor, trong Pug cũng như trong HTML. Hãy viết command-for (wrapper chấp nhận cả hai dạng), hoặc tắt rule đó. Xem tài liệu eslint-plugin-pug-vue về các rule, thiết lập editor và cách dùng cùng oxlint.

Phát hành theo giấy phép MIT. Logo Vue của Evan You, CC BY-NC-SA 4.0.