provide与inject创建目录

main
roger 2 years ago
parent aab48951d0
commit 2030130e51
  1. 31
      20_脚手架/vue3_test/18.src_provide与inject/App.vue
  2. BIN
      20_脚手架/vue3_test/18.src_provide与inject/assets/logo.png
  3. 21
      20_脚手架/vue3_test/18.src_provide与inject/components/Child.vue
  4. 26
      20_脚手架/vue3_test/18.src_provide与inject/components/Son.vue
  5. 10
      20_脚手架/vue3_test/18.src_provide与inject/main.js

@ -0,0 +1,31 @@
<template>
<div className="app">
<h3>我是app组件{{ name }}-{{ price }}</h3>
<child/>
</div>
</template>
<script>
import child from "@/components/Child.vue";
import {reactive, toRefs, provide} from "vue";
export default {
name: 'App',
components: {child},
setup() {
let car = reactive({
name: '奔驰',
price: '40W'
})
provide('car', car) //
return {...toRefs(car)}
}
}
</script>
<style>
.app {
background-color: gray;
padding: 10px;
}
</style>

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.7 KiB

@ -0,0 +1,21 @@
<template>
<div class="child">
<h3>我是child组件</h3>
<son/>
</div>
</template>
<script>
import son from "@/components/Son.vue";
export default {
name: "Child",
components:{son}
}
</script>
<style>
.child{
background-color: skyblue;
padding: 10px;
}
</style>

@ -0,0 +1,26 @@
<template>
<div class="son">
<h3>我是son组件{{car.name}}-{{car.price}}</h3>
</div>
</template>
<script>
import {inject} from "vue";
export default {
name: "Son",
setup() {
let car = inject('car')
console.log('---son---', car)
return {car}
}
}
</script>
<style scoped>
.son{
background-color: orange;
padding: 10px;
}
</style>

@ -0,0 +1,10 @@
// 引入的不再是Vue构造函数, 引入的是一个名为createApp的工厂函数
import { createApp } from 'vue'
import App from './App.vue'
// createApp(App).mount('#app')
// 创建应用实例对象
const app = createApp(App)
console.log('@@@',app)
app.mount('#app')
Loading…
Cancel
Save