Skip to content

Commit 6967229

Browse files
authored
docs: add scoping for custom directive (#1867)
1 parent 639e05e commit 6967229

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

docs/guide/advanced/directive.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ const i18n = createI18n({
7979

8080
const app = createApp({
8181
data() {
82-
return {
82+
return {
8383
byePath: 'message.bye',
8484
appleCount: 7,
8585
}
@@ -112,6 +112,16 @@ Outputs:
112112
</div>
113113
```
114114

115+
## scoping
116+
117+
As mentioned in [the scope section](../essentials/scope.md), vue-i18n has a global scope and a local scope.
118+
119+
The scope under which `v-t` is also affected by scope when it works.
120+
121+
- local scope: using the i18n option in Legacy API style or using `useScope: ‘local'` in `useI18n`.
122+
- global scope: all cases other than the above.
123+
124+
115125
## `$t` vs `v-t`
116126

117127
### `$t`

packages/vue-i18n-core/test/directive.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,36 @@ test('legacy mode', async () => {
165165
expect(wrapper.html()).toEqual('<p>hello!</p>')
166166
})
167167

168+
test('fallback to global scope', async () => {
169+
const i18n = createI18n({
170+
locale: 'en',
171+
messages: {
172+
en: {
173+
hello: 'hello!'
174+
}
175+
}
176+
})
177+
178+
const Child = defineComponent({
179+
setup() {
180+
// <p v-t="'hello'"></p>
181+
const t = resolveDirective('t')
182+
return () => {
183+
return withDirectives(h('p'), [[t!, 'hello']])
184+
}
185+
}
186+
})
187+
188+
const App = defineComponent({
189+
setup() {
190+
return () => h('div', [h(Child)])
191+
}
192+
})
193+
const wrapper = await mount(App, i18n)
194+
195+
expect(wrapper.html()).toEqual('<div><p>hello!</p></div>')
196+
})
197+
168198
test('using in template', async () => {
169199
const i18n = createI18n({
170200
locale: 'en',

0 commit comments

Comments
 (0)