Skip to content

Commit 0c13d59

Browse files
committed
ref guard
1 parent 101f0b1 commit 0c13d59

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

src/register.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import * as _ from 'lodash'
66

77
const hyphenateRE = /([^-])([A-Z])/g
88
const hyphenate = ((str: string): string => {
9-
return str
10-
.replace(hyphenateRE, '$1-$2')
11-
.replace(hyphenateRE, '$1-$2')
12-
.toLowerCase()
9+
return str
10+
.replace(hyphenateRE, '$1-$2')
11+
.replace(hyphenateRE, '$1-$2')
12+
.toLowerCase()
1313
})
1414

1515
export function register_all_components(vue: typeof Vue, prefix: string) {
@@ -40,7 +40,16 @@ export function register_all_methods(vue: typeof Vue, instance) {
4040
mdls[k] = modules[k](instance, this)
4141
}
4242
for (var k in components) {
43-
mdls[_.camelCase(k)] = (ref) => { return this.$refs[ref] }
43+
mdls[_.camelCase(k)] = (ref) => {
44+
if (typeof ref !== 'string')
45+
throw new Error('ref must be string represents the ref name of component')
46+
47+
let component = this.$refs[ref]
48+
if (!component)
49+
throw new Error(`Could not resolve ui component in '${this.$options.name}' referenced by '${ref}'.`)
50+
51+
return component
52+
}
4453
}
4554

4655
mdls = _.merge(mdls, instance)

0 commit comments

Comments
 (0)