shoelace/docs/frameworks/vue.md

101 wiersze
2.6 KiB
Markdown
Czysty Zwykły widok Historia

2021-11-04 11:27:18 +00:00
# Vue
Vue [plays nice](https://custom-elements-everywhere.com/#vue) with custom elements, so you can use Shoelace in your Vue apps with ease.
2022-05-27 13:45:14 +00:00
?> These instructions are for Vue 3 and above. If you're using Vue 2, please see the [Vue 2 instructions](/frameworks/vue-2).
2021-11-12 15:31:13 +00:00
2021-11-04 11:27:18 +00:00
## Installation
To add Shoelace to your Vue app, install the package from npm.
```bash
npm install @shoelace-style/shoelace
```
2021-11-12 15:31:13 +00:00
Next, [include a theme](/getting-started/themes) and set the [base path](/getting-started/installation#setting-the-base-path) for icons and other assets. In this example, we'll import the light theme and use the CDN as a base path.
```jsx
import '@shoelace-style/shoelace/dist/themes/light.css';
import { setBasePath } from '@shoelace-style/shoelace/dist/utilities/base-path';
setBasePath('https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace@%VERSION%/dist/');
```
?> If you'd rather not use the CDN for assets, you can create a build task that copies `node_modules/@shoelace-style/shoelace/dist/assets` into a public folder in your app. Then you can point the base path to that folder instead.
## Configuration
2021-11-04 11:27:18 +00:00
You'll need to tell Vue to ignore Shoelace components. This is pretty easy because they all start with `sl-`.
```js
2022-05-27 13:45:14 +00:00
import { fileURLToPath, URL } from 'url';
2022-05-27 13:41:47 +00:00
2022-05-27 13:45:14 +00:00
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
2022-05-27 13:41:47 +00:00
// https://vitejs.dev/config/
export default defineConfig({
plugins: [
vue({
template: {
compilerOptions: {
isCustomElement: tag => tag.startsWith('sl-')
}
}
})
],
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url))
}
}
2022-05-27 13:45:14 +00:00
});
2021-11-04 11:27:18 +00:00
```
2021-11-12 15:31:13 +00:00
Now you can start using Shoelace components in your app!
## Usage
2022-05-27 13:41:47 +00:00
### QR code generator example
2021-11-04 11:27:18 +00:00
2022-05-27 13:41:47 +00:00
```vue
<template>
<div class="container">
<h1>QR code generator</h1>
2021-11-04 11:27:18 +00:00
2022-05-27 13:41:47 +00:00
<sl-input maxlength="255" clearable label="Value" v-model="qrCode"></sl-input>
2021-11-04 11:27:18 +00:00
2022-05-27 13:41:47 +00:00
<sl-qr-code :value="qrCode"></sl-qr-code>
</div>
</template>
2021-11-04 11:27:18 +00:00
2022-05-27 13:41:47 +00:00
<script setup>
2022-05-27 13:45:14 +00:00
import { ref } from 'vue';
import '@shoelace-style/shoelace/dist/components/qr-code/qr-code.js';
import '@shoelace-style/shoelace/dist/components/input/input.js';
2021-11-04 11:27:18 +00:00
2022-05-27 13:45:14 +00:00
const qrCode = ref();
2022-05-27 13:41:47 +00:00
</script>
2021-11-04 11:27:18 +00:00
2022-05-27 13:41:47 +00:00
<style>
.container {
max-width: 400px;
margin: 0 auto;
}
2021-11-04 11:27:18 +00:00
2022-05-27 13:41:47 +00:00
sl-input {
margin: var(--sl-spacing-large) 0;
}
</style>
2021-11-04 11:27:18 +00:00
```
2022-05-27 13:41:47 +00:00
### Binding Complex Data
2021-11-04 11:27:18 +00:00
2022-05-27 13:41:47 +00:00
When binding complex data such as objects and arrays, use the `.prop` modifier to make Vue bind them as a property instead of an attribute.
2021-11-04 11:27:18 +00:00
```html
2022-05-27 13:41:47 +00:00
<sl-color-picker :swatches.prop="mySwatches" />
2021-11-04 11:27:18 +00:00
```
2021-11-12 15:31:13 +00:00
?> Are you using Shoelace with Vue? [Help us improve this page!](https://github.com/shoelace-style/shoelace/blob/next/docs/frameworks/vue.md)