Skip to content

Commit a6e013e

Browse files
authored
docs: add example for transforming custom blocks (#221)
1 parent 4d417cb commit a6e013e

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

packages/plugin-vue/README.md

+27
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,15 @@ import yaml from 'js-yaml'
140140
const vueI18nPlugin = {
141141
name: 'vue-i18n',
142142
transform(code, id) {
143+
// if .vue file don't have <i18n> block, just return
143144
if (!/vue&type=i18n/.test(id)) {
144145
return
145146
}
147+
// parse yaml
146148
if (/\.ya?ml$/.test(id)) {
147149
code = JSON.stringify(yaml.load(code.trim()))
148150
}
151+
// mount the value on the i18n property of the component instance
149152
return `export default Comp => {
150153
Comp.i18n = ${code}
151154
}`
@@ -157,6 +160,30 @@ export default {
157160
}
158161
```
159162

163+
Create a file named `Demo.vue`, add `lang="yaml"` to the `<i18n>` blocks, then you can use the syntax of `YAML`:
164+
165+
```vue
166+
<template>Hello</template>
167+
168+
<i18n lang="yaml">
169+
message: 'world'
170+
fullWord: 'hello world'
171+
</i18n>
172+
```
173+
174+
`message` is mounted on the i18n property of the component instance, you can use like this:
175+
176+
```vue
177+
<script setup lang="ts">
178+
import Demo from 'components/Demo.vue'
179+
</script>
180+
181+
<template>
182+
<Demo /> {{ Demo.i18n.message }}
183+
<div>{{ Demo.i18n.fullWord }}</div>
184+
</template>
185+
```
186+
160187
## Using Vue SFCs as Custom Elements
161188

162189
> Requires `vue@^3.2.0` & `@vitejs/plugin-vue@^1.4.0`

0 commit comments

Comments
 (0)