<template> <child-component :dataProp="parentData" /> </template> <script> export default { data() { return { parentData: 'Initial data' }; }, methods: { updateChildComponent() { = 'New data'; } } }; </script>
In the child component:
<template> <div>{{ dataProp }}</div> </template> <script> export default { props: ['dataProp'] }; </script>
2. Refer to the subcomponent using $refs:
ref
Create a reference for a child component, and then directly call the child component's methods or access its data by reference.<template> <child-component ref="childRef" /> </template> <script> export default { methods: { updateChildComponent() { // Call the method of the child component or access the data through $refs this.$(); } } }; </script>
In the child component:
<template> <!-- Subcomponent content --> </template> <script> export default { methods: { someMethod() { // Here you can perform the operation of refreshing the subcomponent } } }; </script>
3. Add key value to child components:
After the key value changes, the component will be automatically re-rendered. The key in vue is mainly used to efficiently update the dom. It can also be used to force the element/component to be replaced instead of reusing it. The completed triggers the component's life cycle hook and triggers the transition.
Parent component:
<template> <el-button @click="click">Refresh subcomponents</el-button> <child-component :key="datekey" /> </template> <script> export default{ data(){ return { datekey:() } }, methods:{ click(){ //If the datekey is updated here, the component will be refreshed = () } } } </script>