Skip to content

Commit 25eee91

Browse files
committed
ensure options.el is element when initializing props
1 parent ab8b0e9 commit 25eee91

File tree

3 files changed

+28
-13
lines changed

3 files changed

+28
-13
lines changed

src/api/lifecycle.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,9 @@ exports.$mount = function (el) {
1616
_.warn('$mount() should be called only once.')
1717
return
1818
}
19+
el = _.query(el)
1920
if (!el) {
2021
el = document.createElement('div')
21-
} else if (typeof el === 'string') {
22-
var selector = el
23-
el = document.querySelector(el)
24-
if (!el) {
25-
_.warn('Cannot find element: ' + selector)
26-
return
27-
}
2822
}
2923
this._compile(el)
3024
this._isCompiled = true
@@ -70,4 +64,4 @@ exports.$destroy = function (remove, deferCleanup) {
7064

7165
exports.$compile = function (el, host) {
7266
return compiler.compile(el, this.$options, true, host)(this, el)
73-
}
67+
}

src/instance/scope.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,19 @@ exports._initProps = function () {
2727
var options = this.$options
2828
var el = options.el
2929
var props = options.props
30-
this._propsUnlinkFn = el && props
31-
? compiler.compileAndLinkProps(
32-
this, el, props
33-
)
34-
: null
3530
if (props && !el) {
3631
_.warn(
3732
'Props will not be compiled if no `el` option is ' +
3833
'provided at instantiation.'
3934
)
4035
}
36+
// make sure to convert string selectors into element now
37+
el = options.el = _.query(el)
38+
this._propsUnlinkFn = el && props
39+
? compiler.compileAndLinkProps(
40+
this, el, props
41+
)
42+
: null
4143
}
4244

4345
/**

src/util/dom.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
1+
var _ = require('./index')
12
var config = require('../config')
23

4+
/**
5+
* Query an element selector if it's not an element already.
6+
*
7+
* @param {String|Element} el
8+
* @return {Element}
9+
*/
10+
11+
exports.query = function (el) {
12+
if (typeof el === 'string') {
13+
var selector = el
14+
el = document.querySelector(el)
15+
if (!el) {
16+
_.warn('Cannot find element: ' + selector)
17+
}
18+
}
19+
return el
20+
}
21+
322
/**
423
* Check if a node is in the document.
524
* Note: document.documentElement.contains should work here

0 commit comments

Comments
 (0)