Преглед изворни кода

allow $mount() to auto-create a div when called with no arguments

Evan You пре 11 година
родитељ
комит
ca30122bd1
2 измењених фајлова са 16 додато и 1 уклоњено
  1. 3 1
      src/api/lifecycle.js
  2. 13 0
      test/unit/specs/api/lifecycle_spec.js

+ 3 - 1
src/api/lifecycle.js

@@ -15,7 +15,9 @@ exports.$mount = function (el) {
     _.warn('$mount() should be called only once.')
     return
   }
-  if (typeof el === 'string') {
+  if (!el) {
+    el = document.createElement('div')
+  } else if (typeof el === 'string') {
     var selector = el
     el = document.querySelector(el)
     if (!el) {

+ 13 - 0
test/unit/specs/api/lifecycle_spec.js

@@ -28,6 +28,19 @@ if (_.inBrowser) {
         expect(el.textContent).toBe('hi!')
       })
 
+      it('auto-create', function () {
+        var vm = new Vue({
+          template: '{{a}}',
+          data: {
+            a: 123
+          }
+        })
+        vm.$mount()
+        expect(vm.$el).toBeTruthy()
+        expect(vm.$el.tagName).toBe('DIV')
+        expect(vm.$el.textContent).toBe('123')
+      })
+
       it('selector', function () {
         el.id = 'mount-test'
         document.body.appendChild(el)