Просмотр исходного кода

feat(ssr): inheritAttrs support in SSR

Evan You 9 лет назад
Родитель
Сommit
6bf97721f1
2 измененных файлов с 23 добавлено и 5 удалено
  1. 8 5
      src/platforms/web/server/modules/attrs.js
  2. 15 0
      test/ssr/ssr-string.spec.js

+ 8 - 5
src/platforms/web/server/modules/attrs.js

@@ -17,12 +17,15 @@ export default function renderAttrs (node: VNodeWithData): string {
   let attrs = node.data.attrs
   let attrs = node.data.attrs
   let res = ''
   let res = ''
 
 
-  let parent = node.parent
-  while (isDef(parent)) {
-    if (isDef(parent.data) && isDef(parent.data.attrs)) {
-      attrs = Object.assign({}, attrs, parent.data.attrs)
+  const opts = node.parent && node.parent.componentOptions
+  if (isUndef(opts) || opts.Ctor.options.inheritAttrs !== false) {
+    let parent = node.parent
+    while (isDef(parent)) {
+      if (isDef(parent.data) && isDef(parent.data.attrs)) {
+        attrs = Object.assign({}, attrs, parent.data.attrs)
+      }
+      parent = parent.parent
     }
     }
-    parent = parent.parent
   }
   }
 
 
   if (isUndef(attrs)) {
   if (isUndef(attrs)) {

+ 15 - 0
test/ssr/ssr-string.spec.js

@@ -880,6 +880,21 @@ describe('SSR: renderToString', () => {
       done()
       done()
     })
     })
   })
   })
+
+  it('with inheritAttrs: false + $attrs', done => {
+    renderVmWithOptions({
+      template: `<foo id="a"/>`,
+      components: {
+        foo: {
+          inheritAttrs: false,
+          template: `<div><div v-bind="$attrs"></div></div>`
+        }
+      }
+    }, res => {
+      expect(res).toBe(`<div data-server-rendered="true"><div id="a"></div></div>`)
+      done()
+    })
+  })
 })
 })
 
 
 function renderVmWithOptions (options, cb) {
 function renderVmWithOptions (options, cb) {