Skip to content

Commit 0a72913

Browse files
authored
Merge pull request #1320 from earnubs/template-query-selector-support
Template querySelector support
2 parents 0e97717 + 7b25c5a commit 0a72913

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

packages/shared/compile-template.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ export function compileFromString(str: string) {
1717

1818
export function compileTemplate(component: Component): void {
1919
if (component.template) {
20+
if (component.template.charAt('#') === '#') {
21+
var el = document.querySelector(component.template)
22+
if (!el) {
23+
throwError('Cannot find element' + component.template)
24+
25+
el = document.createElement('div')
26+
}
27+
component.template = el.innerHTML
28+
}
29+
2030
Object.assign(component, compileToFunctions(component.template))
2131
}
2232

test/specs/mount.spec.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,25 @@ describeRunIf(process.env.TEST_ENV !== 'node', 'mount', () => {
161161
expect(wrapper.html()).to.equal(`<div>foo</div>`)
162162
})
163163

164+
itDoNotRunIf(
165+
!(navigator.userAgent.includes && navigator.userAgent.includes('node.js')),
166+
'compiles templates from querySelector',
167+
() => {
168+
const template = window.createElement('div')
169+
template.setAttribute('id', 'foo')
170+
template.innerHTML = '<div>foo</div>'
171+
window.document.body.appendChild(template)
172+
173+
const wrapper = mount({
174+
template: '#foo'
175+
})
176+
expect(wrapper.vm).to.be.an('object')
177+
expect(wrapper.html()).to.equal(`<div>foo</div>`)
178+
179+
window.body.removeChild(template)
180+
}
181+
)
182+
164183
itDoNotRunIf(vueVersion < 2.3, 'overrides methods', () => {
165184
const stub = sandbox.stub()
166185
const TestComponent = Vue.extend({

0 commit comments

Comments
 (0)