Bỏ qua để đến nội dung

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.

Terminal window
npm install @wink/elements
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-'),
},
},
}),
],
});
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”
src/main.ts
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');
<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:

src/wink-elements.d.ts
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;
}
}

Thêm vào .env.local:

VITE_WINK_CLIENT_ID=your-client-id

Vite chỉ cung cấp các biến có tiền tố VITE_ cho gói client.