Vue: Recursive single file components' rendering functions are not defined

Created on 4 Nov 2016  ·  2Comments  ·  Source: vuejs/vue

Vue.js version

2.0.2

Reproduction Link

https://github.com/MarcoBoffo/vue-test-render-bug

Steps to reproduce

Run the build and check the browser's console log

What is Expected?

Blocks should render normally or, at least, give another warn

What is actually happening?

Vue says the template or render function is not defined

vue.common.js?e881:2611[Vue warn]: Failed to mount component: template or render function not defined. (found in component <block>)

Most helpful comment

This is due to the nature of cyclic dependencies in a module system. When DiagramPath imports Block, Block hasn't finished evaluating yet (because it in turn depends on DiagramPath), so it gets an empty object instead.

What you can do is using a dynamic require() to lazy-load Block in DiagramPath:

export default {
  beforeCreate () {
    this.$options.components.Block = require('./Block.vue')
  }
}

All 2 comments

This is due to the nature of cyclic dependencies in a module system. When DiagramPath imports Block, Block hasn't finished evaluating yet (because it in turn depends on DiagramPath), so it gets an empty object instead.

What you can do is using a dynamic require() to lazy-load Block in DiagramPath:

export default {
  beforeCreate () {
    this.$options.components.Block = require('./Block.vue')
  }
}

Wouldn't it be nice if we just put this note here?
https://vuejs.org/v2/guide/components.html#Recursive-Component

Was this page helpful?
0 / 5 - 0 ratings