question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

vuejs app architecture for dynamic template with dynamic data and dynamic component type issue

See original GitHub issue

Sorry for posting ‘issue’ here. I have posted question on vue forum and stackoverflow but with no feedback. I am now developing an app with vuejs and the architecture is important from begining. Here is the link: http://forum.vuejs.org/topic/4604/how-to-address-component-with-dynamic-template-and-its-relative-data http://stackoverflow.com/questions/38342241/vuejs-better-way-to-address-component-with-dynamic-template-and-its-relative-dat I copy here for kind proposal. It is an architecture issue. I have following question/requirement, if anyone has good idea, please kindly post for discussion It is for the dynamically rendering for dynamical template and data use component The scenario is as following: 1.root vue instance fetch data from backend, say following data returned:

[
{
    /* this section will be fed to a component */
    type: 'FirstSub',
    templatestr: '<div> <more-vue-customized-component itsdata="xxx"></more-vue-customized-component>  </div>',
    data: [
        datafordynamicComponent: xxxx
    ]

},
...
{
    /* this section will be fed to a component */
    type: 'lastSub',
    templatestr: '<p> <more-vue-customized-component itsdata="xxx"></more-vue-customized-component>  </p>',
    data: [
        datafordynamicComponent: xxxx
    ]

}
  1. Which structure/architecture we can use to render as following composition using above data?
<root>
<FirstSub></FirstSub>
...
<lastSub></lastSub>
</root>

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:13 (3 by maintainers)

github_iconTop GitHub Comments

6reactions
terrydaicommented, Jul 19, 2016

Here is a related issue with feature request https://github.com/vuejs/vue/issues/2114.

I have wrote a component-proxy to resolve it which may do some help to you.

import _ from 'lodash'
import Vue from 'vue'


export default {
  props: ['name', 'props', 'value'],
  template:  '',
  created(){
    let props = []
    if(this.props ){
      // apply properties to component data
      _.forOwn(this.props, (value, key)=>{
        // console.debug( key,  this[key])
        props.push(`:${_.kebabCase(key)}="props['${key}']"`)
      })
    }

    // console.debug('component proxy', props.join(' '))
    this.$options.template = `<component :is="name" ${props.join(' ')} :value.sync="value"></component>`
  },
}



About usage:

.ui.form
    .field(v-for="column in table.columns")
      label {{column.title}}
      component-proxy(:name="column.editor.type",:props="column.editor.propsData",:value.sync="column.name")
4reactions
fnlctrlcommented, Jul 13, 2016

Does this solve your problem?

<component v-for="item in yourComponentsFromAPI" :is="item.name" :some-prop="item.data">
</component>
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to render dynamic component defined in JSON using ...
With Vue.js it is unbelievably easy to render dynamic components, which we can utilize to render content with specific components and layouts by...
Read more >
How to make your components dynamic in Vue.js
This post serves as an introduction to the ways you can reference HTML elements in your components in Vue. js. You can toggle...
Read more >
Fully dynamic data-table component for Vue.JS
This article is the first part of this project, the next part will focus on deployment and making this table component reachable as...
Read more >
Help me improve my dynamic component rendering : r/vuejs
Hey Vue experts, I'm fiddling around with Vue 3 and a CMS backend. The intent is to create dynamic components in the CMS...
Read more >
Step 11: Finishing Budgets with Vue.js Dynamic Components
Dynamic components allow the developer to insert a component placeholder in the template of another component. This parent component can then ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found