首页 > 其他分享 >19:vue3 依赖注入

19:vue3 依赖注入

时间:2023-07-12 11:46:51浏览次数:40  
标签:vue return Parent 19 vue3 依赖 export Child msg

1、通过Prop 逐级透传问题(传统老的方法只能逐级传递)

 传统方式代码如下:

App.vue

 1 <template>
 2   <h3>祖宗</h3>
 3   <Parent :msg="msg"></Parent>
 4 </template>
 5 
 6 <script>
 7 import Parent from "./components/Parent.vue"
 8 export default{
 9     data(){
10       return{
11         msg:"祖宗来的消息"
12       }
13     },
14     
15    components:{
16     Parent
17    }
18 }
19 </script>

Parent.vue

 1 <template>
 2     <h3>Parent</h3>
 3     {{ msg }}
 4 
 5    <Child :msg="msg"></Child>
 6 </template>
 7 
 8 <script>
 9 import Child from './Child.vue';
10 export default{
11     data(){
12       return{
13         
14       }
15     },
16     
17    components:{
18     Child
19    },
20    props:["msg"]
21 }
22 </script>

Child.vue

 1 <template>
 2     <h3>Child</h3>
 3    {{ msg }}
 4 </template>
 5 
 6 <script>
 7 export default{
 8     data(){
 9       return{
10        
11       }
12     },
13     
14    props:["msg"]
15 }
16 </script>

效果截图:

2、通过Prop 逐级透传问题

逐级透传,当层级少还可以用用,层级多了就无法使用了。下面使用Provide和inject方式依赖注入实现

 App.vue

 1 <template>
 2   <h3>祖宗</h3>
 3   <Parent></Parent>
 4 </template>
 5 
 6 <script>
 7 import Parent from "./components/Parent.vue"
 8 export default{
 9     data(){
10       return{
11         msg:"祖宗来的消息"
12       }
13     },
14    components:{
15     Parent
16    },
17    provide(){
18     return {
19       message: "祖宗的财产!",
20       msg:this.msg
21     }
22     
23    }
24 }
25 </script>
Parent.vue
 1 <template>
 2     <h3>Parent</h3>
 3    <Child></Child>
 4 </template>
 5 
 6 <script>
 7 import Child from './Child.vue';
 8 export default{
 9     data(){
10       return{
11         
12       }
13     },
14     
15    components:{
16     Child
17    }
18 }
19 </script>
Child.vue
 1 <template>
 2     <h3>Child</h3>
 3    <p>{{ message }}</p>
 4    <P>{{ funMessage }}</P>
 5    <p>{{ msg }}</p>
 6 </template>
 7 
 8 <script>
 9 export default{
10     data(){
11       return{
12         funMessage:this.message
13       }
14     },
15     inject: ["message",'msg']
16 }
17 </script>

效果:

 

--------------

在main.js加入全局

 Child.vue

 

效果:

 提示:

provide和inject只能由上到下的传递,不能反向传递

 

 

 

标签:vue,return,Parent,19,vue3,依赖,export,Child,msg
From: https://www.cnblogs.com/wuzexin/p/17547106.html

相关文章

  • Maven enforcer插件依赖收敛问题
    当项目中引入新的jar包或者升级jar后,重新构建Maven项目时常出现这种报错:Failedtoexecutegoalorg.apache.maven.plugins:maven-enforcer-plugin:3.0.0-M2:enforce(enforce-rules)onprojectxxx-xxxx:SomeEnforcerruleshavefailed. 报错信息  从报错信息看和M......
  • 7-11 leetcode 2619
    请你编写一段代码实现一个数组方法,使任何数组都可以调用 array.last() 方法,这个方法将返回数组最后一个元素。如果数组中没有元素,则返回 -1 。ps:this环境变量的使用,this.length的返回值是数字类型代码实现:<script>//在数组的原型写扩展方法可以给所有的数组......
  • 注解开发依赖注入
        ......
  • 力扣---1911. 最大子序列交替和
    一个下标从 0 开始的数组的 交替和 定义为 偶数 下标处元素之 和 减去 奇数 下标处元素之 和 。比方说,数组 [4,2,5,3] 的交替和为 (4+5)-(2+3)=4 。给你一个数组 nums ,请你返回 nums 中任意子序列的 最大交替和 (子序列的下标 重新 从0开始......
  • P6109 [Ynoi2019] rprmq1
    LuoguP6109[Ynoi2009]rprmq1LuoguP6109题目背景我谔谔本题读入量约13MB,输出量约7MB,请选择合适的输入输出方法题目描述有一个\(n\timesn\)的矩阵\(a\),初始全是\(0\),有\(m\)次修改操作和\(q\)次查询操作,先进行所有修改操作,然后进行所有查询操作。一次修改......
  • 18:vue3 异步加载
    在大型项目中,我们可能需要拆分应用为更小的块,并仅在需要时再从服务器加载相关组件。Vue提供了 defineAsyncComponent 方法来实现此功能: 1<template>2<h3>异步加载</h3>3<KeepAlive>4<component:is="tabComponent"></component>5</KeepAlive>......
  • 16:vue3 动态组件
    A组件1<template>2<h3>ComponentA</h3>3</template>B组件1<template>2<h3>ComponentB</h3>3</template> App.vue1<template>2<h3>动态组件(A、B两个组件动态切换)</h3>3<compo......
  • Windows10无法上网网络提示"依赖或组无法启动"的处理方法
    参考:https://www.haozhuangji.com/xtjc/111319342.htmlwin+r输入msconfig选择服务-全部启用重新启动......
  • pom.xml增加Mybatisplus的依赖后报错:Factory method 'sqlSessionFactory' threw excep
    出现上面的问题,是版本不匹配导致。<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.2.0</version></dependency&......
  • 15:vue3 组件生命周期函数应用
    1<template>2<h3>组件生命周期函数应用</h3>3<!--验证Dom结构加载时机-->4<pref="name">我的内容</p>5<!--模拟网络加载数据-->6<ul>7<liv-for="(item,index)inbanner":key="item.id&q......