123456789101112131415161718192021222324252627282930 |
- <template>
- <h2>provide 和 inject</h2>
- <p>当前的颜色: {{ color }}</p>
- <button @click="color='red'">红色</button>
- <button @click="color='yellow'">黄色</button>
- <button @click="color='green'">绿色</button>
- <hr>
- <Child></Child>
- </template>
- <script lang="ts">
- import { defineComponent, provide, ref } from "vue";
- import Child from "./components/Child.vue"
- export default defineComponent({
- //实现跨层级组件(祖孙)之间通信
- setup() {
- const color = ref("red");
- provide('color',color)
- return {
- color,
- };
- },
- components: {
- Child
- }
- });
- </script>
- <style scoped>
- </style>
|