You have two separate components here, the Header component and the anonymous component you created (with new Vue()). The anonymous component renders the Header component as its child. Both components have separate data properties; the parent has text = 'New header text' and the child has text = 'Header text'.
There are two ways (off the top of my head) that you can achieve this:
- Extend the Header component and override its
text data property with the new value:
const Header = require('components/header.vue');
const HeaderComp = Vue.extend(Header);
const header = new HeaderComp({
el: '#header',
data: {
text: 'New header text',
},
});
- By making
text a prop instead, you will be able to pass data down to the child from the parent:
header.vue
<script>
export default {
props: ['text'],
};
</script>
usage
const Header = require('components/header.vue');
// Using render function
const header = new Vue({
el: '#header',
render: h => h(Header, {
props: {
text: 'New header text',
},
}),
});
// Using template
const header = new Vue({
el: '#header',
components: {
Header,
},
template: '<header text="New header text"/>',
});
Vue.extend()the Header component, then instantiate that with an overridden data property.const Header = Vue.extend(require('components/header.vue'));and thennew Header({ el: ..., data: { ... } });, right? This works, but it looks a little weird extending the import. Is this really necessary?