Vue 3
Vue 3 cần một gợi ý trình biên dịch để xử lý các thẻ <wink-*> như các phần tử tùy chỉnh gốc thay vì các thành phần Vue chưa được giải quyết. Sau đó, chúng hoạt động như bất kỳ phần tử HTML nào khác.
@wink/elements npm package Cài đặt và tải gói CDN kèm kiểu TypeScript.
Cài đặt
Phần tiêu đề “Cài đặt”npm install @wink/elementsCấu hình trình biên dịch
Phần tiêu đề “Cấu hình trình biên dịch”Vite + Vue (vite.config.ts)
Phần tiêu đề “Vite + Vue (vite.config.ts)”import { defineConfig } from 'vite';import vue from '@vitejs/plugin-vue';
export default defineConfig({ plugins: [ vue({ template: { compilerOptions: { // Xử lý tất cả các thẻ bắt đầu bằng "wink-" như phần tử tùy chỉnh isCustomElement: (tag) => tag.startsWith('wink-'), }, }, }), ],});Vue CLI (vue.config.js)
Phần tiêu đề “Vue CLI (vue.config.js)”module.exports = { chainWebpack(config) { config.module .rule('vue') .use('vue-loader') .tap((options) => ({ ...options, compilerOptions: { isCustomElement: (tag) => tag.startsWith('wink-'), }, })); },};Tải CDN một lần khi khởi động ứng dụng
Phần tiêu đề “Tải CDN một lần khi khởi động ứng dụng”import { createApp } from 'vue';import App from './App.vue';import { load } from '@wink/elements';
load({ clientId: import.meta.env.VITE_WINK_CLIENT_ID });
createApp(App).mount('#app');Sử dụng trong template
Phần tiêu đề “Sử dụng trong template”<template> <main> <wink-content-loader layout="HOTEL" :id="layoutId" /> <wink-lookup /> </main></template>
<script setup lang="ts">const layoutId = 'YOUR_LAYOUT_ID';</script>TypeScript — mở rộng kiểu phần tử toàn cục
Phần tiêu đề “TypeScript — mở rộng kiểu phần tử toàn cục”Vue 3 + TypeScript đọc kiểu phần tử tùy chỉnh từ HTMLElementTagNameMap toàn cục. Thêm một file khai báo:
import type { WinkContentLoaderAttributes, WinkLookupAttributes, WinkSearchButtonAttributes, WinkAccountButtonAttributes, WinkItineraryButtonAttributes, WinkShoppingCartButtonAttributes, WinkAppLoaderAttributes,} from '@wink/elements';
declare global { interface HTMLElementTagNameMap { 'wink-content-loader': HTMLElement & WinkContentLoaderAttributes; 'wink-lookup': HTMLElement & WinkLookupAttributes; 'wink-search-button': HTMLElement & WinkSearchButtonAttributes; 'wink-account-button': HTMLElement & WinkAccountButtonAttributes; 'wink-itinerary-button': HTMLElement & WinkItineraryButtonAttributes; 'wink-shopping-cart-button': HTMLElement & WinkShoppingCartButtonAttributes; 'wink-app-loader': HTMLElement & WinkAppLoaderAttributes; }}Biến môi trường
Phần tiêu đề “Biến môi trường”Thêm vào .env.local:
VITE_WINK_CLIENT_ID=your-client-idVite chỉ cung cấp các biến có tiền tố VITE_ cho gói client.