vue3 provide与inject的使用与详情

定义值,获取值

  • provide第一个参数是key,第二个参数是value
  • inject第一个参数是获取provide的key,从而获取value ,第二个值如未获取到相应的值则给一个默认值

provide 传递参数 inject获取参数 例如:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="http://unpkg.com/vue@next"></script>
</head>

<body>
    <div id="root"></div>
</body>
<script>
    let app = Vue.createApp({
        template: '<child ></child>',
        setup() {
            const {
                provide,
                ref
            } = Vue
            provide('name', 'aa')
        },
    })
    app.component('child', {
        template: '<div >{{aa}}</div>',
        setup(props, context) {
            const {
                inject
            } = Vue
            const aa = inject('name', 'hello') //第二个参数是默认值如果没有则默认值
            return {
                aa
            }
        },
    })
    app.mount('#root')
</script>

</html>

inject修改provide传递过来的值 (不建议直接修改,项目中子组件数据与父组件数据最好都独立)

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="http://unpkg.com/vue@next"></script>
</head>
<body>
    <div id="root"></div>
</body>
<script>
    let app = Vue.createApp({
        template: '<child ></child>',
        setup() {
            const {
                provide,
                ref
            } = Vue
            let name = ref('aa')
            provide('name', name)
        },
    })
    app.component('child', {
        template: '<div @click="handleChange">{{name}}</div>',
        setup(props, context) {
            const {
                inject
            } = Vue
            const name = inject('name')
            const handleChange = () => {
                name.value = '测试'
            }
            return {
                name,
                handleChange
            }
        },
    })
    app.mount('#root')
</script>

</html>

inject组件修改provide组件数据(推荐写法通过函数)

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="http://unpkg.com/vue@next"></script>
</head>

<body>
    <div id="root"></div>
</body>
<script>
    let app = Vue.createApp({
        template: '<child ></child>',
        setup() {
            const {
                provide,
                ref
            } = Vue
            let name = ref('aa')
            provide('name', name)
            provide('change', (val) => {
                name.value = val
            })
        },
    })
    app.component('child', {
        template: '<div @click="handleChange">{{name}}</div>',
        setup(props, context) {
            const {
                inject
            } = Vue
            const change = inject('change')
            const handleChange = () => {
                change('测试2')
            }
            const name = inject('name')
            return {
                name,
                handleChange
            }
        },
    })
    app.mount('#root')
</script>

</html>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值