Skip to content

docs: add scoping for custom directive #1867

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion docs/guide/advanced/directive.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ const i18n = createI18n({

const app = createApp({
data() {
return {
return {
byePath: 'message.bye',
appleCount: 7,
}
Expand Down Expand Up @@ -112,6 +112,16 @@ Outputs:
</div>
```

## scoping

As mentioned in [the scope section](../essentials/scope.md), vue-i18n has a global scope and a local scope.

The scope under which `v-t` is also affected by scope when it works.

- local scope: using the i18n option in Legacy API style or using `useScope: ‘local'` in `useI18n`.
- global scope: all cases other than the above.


## `$t` vs `v-t`

### `$t`
Expand Down
30 changes: 30 additions & 0 deletions packages/vue-i18n-core/test/directive.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,36 @@ test('legacy mode', async () => {
expect(wrapper.html()).toEqual('<p>hello!</p>')
})

test('fallback to global scope', async () => {
const i18n = createI18n({
locale: 'en',
messages: {
en: {
hello: 'hello!'
}
}
})

const Child = defineComponent({
setup() {
// <p v-t="'hello'"></p>
const t = resolveDirective('t')
return () => {
return withDirectives(h('p'), [[t!, 'hello']])
}
}
})

const App = defineComponent({
setup() {
return () => h('div', [h(Child)])
}
})
const wrapper = await mount(App, i18n)

expect(wrapper.html()).toEqual('<div><p>hello!</p></div>')
})

test('using in template', async () => {
const i18n = createI18n({
locale: 'en',
Expand Down
Loading