Passing props dynamically to dynamic component in VueJS

后端 未结 5 1390
南笙
南笙 2020-12-02 05:44

I\'ve a dynamic view:

with an associated Vue ins

5条回答
  •  情深已故
    2020-12-02 06:25

    To pass props dynamically, you can add the v-bind directive to your dynamic component and pass an object containing your prop names and values:

    So your dynamic component would look like this:

    
    

    And in your Vue instance, currentProperties can change based on the current component:

    data: function () {
      return {
        currentComponent: 'myComponent',
      }
    },
    computed: {
      currentProperties: function() {
        if (this.currentComponent === 'myComponent') {
          return { foo: 'bar' }
        }
      }
    }   
    

    So now, when the currentComponent is myComponent, it will have a foo property equal to 'bar'. And when it isn't, no properties will be passed.

提交回复
热议问题