祖孙组件的传递Provide 和 Inject
祖先 : <template> <div class="app"> <home></home> <h2>App: {{ message }}</h2> <button @click="message = 'hello world'">修改message</button> </div> </template> <script> import { computed } from 'vue' import Home from './Home.vue' export default { components: { Home }, created() { }, data() { return { message: "Hello App" } }, // provide一般都是写成函数 provide() { return { name: "why", age: 18, message: computed(() => this.message) } } } </script> <style scoped> </style>
孙子 : <template> <div class="banner"> <h2>HomeBanner: {{ name }} - {{ age }} - {{message.value}}</h2> </div> </template> <script> export default { inject: ["name", "age", "message"] } </script> <style scoped> </style>
本文来自博客园,作者:杨建鑫,转载请注明原文链接:https://www.cnblogs.com/qd-lbxx/p/16614967.html