From 2ddead03f605485f80b2f17975c18142bfc9e3cf Mon Sep 17 00:00:00 2001 From: PainScript <35187547+Trendymen@users.noreply.github.com> Date: Tue, 5 Jan 2021 19:29:22 +0800 Subject: [PATCH] fix: useCSSModule to adapt the change of getCurrentInstance, close #620 (#622) * fix(useCssModule): adapting the behavior of `useCSSModule` to the change of `getCurrentInstance`. close #620 * chores: add test for `useCssModule`. --- src/apis/useCssModule.ts | 2 +- test/apis/useCssModule.spec.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 test/apis/useCssModule.spec.js diff --git a/src/apis/useCssModule.ts b/src/apis/useCssModule.ts index 368abd77..335c3d29 100644 --- a/src/apis/useCssModule.ts +++ b/src/apis/useCssModule.ts @@ -12,7 +12,7 @@ export const useCSSModule = (name = '$style'): Record => { return EMPTY_OBJ } - const mod = (instance as any)[name] + const mod = (instance.proxy as any)?.[name] if (!mod) { __DEV__ && warn(`Current instance does not have CSS module named "${name}".`) diff --git a/test/apis/useCssModule.spec.js b/test/apis/useCssModule.spec.js new file mode 100644 index 00000000..63c40a83 --- /dev/null +++ b/test/apis/useCssModule.spec.js @@ -0,0 +1,31 @@ +const Vue = require('vue/dist/vue.common.js') +const { useCSSModule } = require('../../src') + +const style = { whateverStyle: 'whateverStyle' } + +function injectStyles() { + Object.defineProperty(this, '$style', { + configurable: true, + get: function () { + return style + }, + }) +} + +describe('api/useCssModule', () => { + it('should get the same object', (done) => { + const vm = new Vue({ + beforeCreate() { + injectStyles.call(this) + }, + template: '
{{style}}
', + setup() { + const style = useCSSModule() + return { style } + }, + }) + vm.$mount() + expect(vm.style).toBe(style) + done() + }) +})