Вам нужно отобразить данные от родителя и передать их ребенку, вот и все!
В примере я делаю передачу строки html и привязки, которую html получил через prop 'fromParentHtml', сопоставленный на child, поэтому внутри дочернего компонента this.fromParentHtml передаётся в существует, потому что он определен в props, и каждый раз, когда вы нажимаете в родительской кнопке, выполняется Функция 'show' и изменить значение с пропущенной проповеди на дочернюю через родительские данные 'html' .. =)
<template>
<div>
Current html sent to child '{{html}}'
<br>
<button @click="show('banana')">Banana</button>
<button @click="show('orange')">Orange</button>
<button @click="show('apple')">Apple</button>
<!-- Create child component -->
<child-component :fromParentHtml="html"></child-component>
</div>
</template>
<script>
export default {
name: "test3",
components: {
'child-component': {
template: "<div>Child component... <br> <span v-html='fromParentHtml'></span> </div>",
//Child component map a prop to receive the sent html from parent through the attribute :fromParentHtml ...
props: {
fromParentHtml: {
required: true,
type: String
}
}
}
},
data(){
return {
html: ''
}
},
methods: {
show(fruit){
this.html = '<span>The fruit is ' + fruit + ' !</span>';
}
}
}
</script>
<style scoped>
</style>
Если вам помогли, отметьте как правильный ответ! Надеюсь, это поможет.
Редактировать 1:
Предполагая, что у вас есть веб-пакет для работы с отдельными компонентами файла, для импорта другого компонента просто выполните:
<template>
<div>
<my-child-component></my-child-component>
</div>
</template>
<script>
//Import some component from a .vue file
import ChildComponent from "./ChildComponent.vue";
export default {
components: {
//And pass it to your component components data, identified by 'my-child-component' in the template tag, just it.
'my-child-component': ChildComponent
},
data(){
},
methods: {
}
}
</script>