From c120436ddb4db58b6e38f03c76daa047b3ef8457 Mon Sep 17 00:00:00 2001 From: Plumbiu <3434909403@qq.com> Date: Fri, 4 Aug 2023 20:46:23 +0800 Subject: [PATCH] docs: add example for transforming custom blocks --- packages/plugin-vue/README.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/packages/plugin-vue/README.md b/packages/plugin-vue/README.md index d5b45a63..22c54bca 100644 --- a/packages/plugin-vue/README.md +++ b/packages/plugin-vue/README.md @@ -140,12 +140,15 @@ import yaml from 'js-yaml' const vueI18nPlugin = { name: 'vue-i18n', transform(code, id) { + // if .vue file don't have block, just return if (!/vue&type=i18n/.test(id)) { return } + // parse yaml if (/\.ya?ml$/.test(id)) { code = JSON.stringify(yaml.load(code.trim())) } + // mount the value on the i18n property of the component instance return `export default Comp => { Comp.i18n = ${code} }` @@ -157,6 +160,30 @@ export default { } ``` +Create a file named `Demo.vue`, add `lang="yaml"` to the `` blocks, then you can use the syntax of `YAML`: + +```vue + + + +message: 'world' +fullWord: 'hello world' + +``` + +`message` is mounted on the i18n property of the component instance, you can use like this: + +```vue + + + +``` + ## Using Vue SFCs as Custom Elements > Requires `vue@^3.2.0` & `@vitejs/plugin-vue@^1.4.0`