1

Code in the file:

<template>
  <component v-bind:is="bbc"></component>
</template>

<script>

import bbc from './bbc.vue';

  export default {
    name: 'ShowRoom2',
  };
</script>

./bbc.vue

<script>
  export default {
    name: 'bbc',
    props: {
      msg: String,
    },
    mounted() {
      console.log('bbc is mounted');
    },
    render() {
      if (this.func) this.func();
      return (
        <div class="bbcMyClass">
          <h1>bbc: <span>Pal</span> <span>{this.msg}</span></h1>
        </div>
      )
    }
  };
</script>

To reproduce

  1. git clone [email protected]:adamchenwei/vue-hoc-playground.git
  2. go to src/components/ShowRoom2.vue
  3. yarn install && yarn serve
  4. observe error in the local browser

enter image description here

2
  • i think you have to set is without : Commented Oct 19, 2018 at 16:51
  • @boussadjrabrahim sorry, I may misunderstood what you said, but document says otherwise (granted, Vue document is not very up to date for some parts) vuejs.org/v2/guide/components-dynamic-async.html Commented Oct 19, 2018 at 16:54

1 Answer 1

3

Yes, the scope in the template is not the same as the script scope. If you need some data, you need to declare it inside the 'component' definition part of the code. For your case, I guess the 'data' property should work

import bbc from './bbc.vue';

export default {
  name: 'ShowRoom2',
  data() {
    return {
      bbc: bbc,
    };
  },
};

However, the template part of your code also looks weird. Could you explain what you're trying to do ?

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

3 Comments

hi, thanks for the answer. I am trying to use dynamic component as a alternative to create HOC, which involving adding class name and such into a wrapped component, which I suppose that <component> thingie can help to do? so next step I am going to do is <component class="something" data-something="somethingelse"...><...>
HOC ? I'm not familiar with that.
it kinda does similiar stuff! I am going for this approach instead of HOC for now (since its not very well implemented in Vuejs, various articles show that). Thanks!

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.