1

I try to create dynamic Vue component with a parent Vue component, but the dynamic component is not created,

I tried to add dynamic component to HTML by add new Child but is not working,,

<template>
  <div class="app-body row">
    <div class="widgets">
      <h1>new ems</h1>

      <div id="Device"><p>ems</p></div>

    </div>
  </div>
</template>

<script>
import { Component, Vue, Watch } from "vue-property-decorator";

export default {

created() {
    console.log('created');
this.displayWidget('Device');
},
methods:{
displayWidget(which) {
    let Child = Vue.extend({
        name: which,
        parent: this,

    });
    new Child({
        el: $(this.$el).find('.widgets')[0],
        template: '<div style="height: 200px; width: 200px; color: yellow">Hello</div>', // tried just standard template stuff here
        render: h => h('div')
    }).$mount();
    }
    }
    }

   </script>

I get error: [Vue warn]:Error in created hook: "ReferenceError: $ is not defined"

1
  • $ is jquery : it's seems jquery is not imported or referenced... Commented Oct 6, 2019 at 13:25

1 Answer 1

1

Try giving the element an id like id="widgets" and use el: '#widgets' - seems to work in this codepen

You could also add a ref to the element like this

<div ref="widgets">

and then

el: this.$refs.widgets,

The reason you're getting the ReferenceError is probably because you haven't imported JQuery

Sign up to request clarification or add additional context in comments.

1 Comment

now I get another error: Cannot find element: #widgets, also I try your example exactly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.