diff --git a/packages/shared/compile-template.js b/packages/shared/compile-template.js index f5057024f..7999894bc 100644 --- a/packages/shared/compile-template.js +++ b/packages/shared/compile-template.js @@ -17,6 +17,16 @@ export function compileFromString(str: string) { export function compileTemplate(component: Component): void { if (component.template) { + if (component.template.charAt('#') === '#') { + var el = document.querySelector(component.template) + if (!el) { + throwError('Cannot find element' + component.template) + + el = document.createElement('div') + } + component.template = el.innerHTML + } + Object.assign(component, compileToFunctions(component.template)) } diff --git a/test/specs/mount.spec.js b/test/specs/mount.spec.js index 9bf2493d7..3a7a78fb3 100644 --- a/test/specs/mount.spec.js +++ b/test/specs/mount.spec.js @@ -161,6 +161,25 @@ describeRunIf(process.env.TEST_ENV !== 'node', 'mount', () => { expect(wrapper.html()).to.equal(`
foo
`) }) + itDoNotRunIf( + !(navigator.userAgent.includes && navigator.userAgent.includes('node.js')), + 'compiles templates from querySelector', + () => { + const template = window.createElement('div') + template.setAttribute('id', 'foo') + template.innerHTML = '
foo
' + window.document.body.appendChild(template) + + const wrapper = mount({ + template: '#foo' + }) + expect(wrapper.vm).to.be.an('object') + expect(wrapper.html()).to.equal(`
foo
`) + + window.body.removeChild(template) + } + ) + itDoNotRunIf(vueVersion < 2.3, 'overrides methods', () => { const stub = sandbox.stub() const TestComponent = Vue.extend({