uniapp - sync关键字

.sync修饰字在vue1.0作为双向数据绑定而存在,在2.0时被砍掉。而后在2.3被当做语法糖加入 (vue单向绑定数据流通)

 

假设一个需求是:父组件值传入到子组件,然后子组件更改后父组件的值也得更改

 

1. 通过props传值,data赋值的操作实现(避免出现双向数据绑定错误)

index.vue

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<template>
    <view class="content">
        <onA :number.sync="num"></onA>
        父组件:{{num}}
    </view>
</template>
 
<script>
    import onA from '@/components/onA.vue';
    export default {
        data() {
            return {
                num: 10
            }
        },
        components: {
            onA
        },
        onLoad() {},
        mounted() {
 
        },
        methods: {
        }
    }
</script>
 
<style>
    .title {
        color: #007AFF;
    }
</style>

 

onA.vue

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<template>
    <view @click="fn">
        子组件:
        {{numberCache}}
    </view>
</template>
 
<script>
    export default {
        data() {
            return {
                numberCache: null
            }
        },
        props: ['number'],
        created() {
            this.numberCache = this.number;
        },
        methods: {
            fn() {
                // 这里处理它就好
                this.numberCache++;
                this.$emit('update:number', this.numberCache);
            }
        },
        mounted() {
 
        }
    }
</script>
 
<style lang="scss" scoped>
    .title {
        color: #F0AD4E;
    }
</style>

  

我们也可以利用子组件this.$emit传值再赋值的形式去更新父组件的值.

 

2. 如果不想引起双向数据绑定报错可以通过ref标识向子组件赋值

 


index.vue(父组件)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<template>
    <view class="content">
        <onA :number.sync="num" ref="on"></onA>
        父组件:{{num}}
    </view>
</template>
 
<script>
    import onA from '@/components/onA.vue';
    export default {
        data() {
            return {
                num: 10
            }
        },
        components: {
            onA
        },
        onLoad() {},
        mounted() {
            this.$refs.on._data.number = this.num;
        },
        methods: {}
    }
</script>
 
<style>
    .title {
        color: #007AFF;
    }
</style>

 

onA.vue

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<template>
    <view @click="fn">
        子组件:
        {{number}}
    </view>
</template>
 
<script>
    export default {
        data() {
            return {
                number: null
            }
        },
        methods: {
            fn() {
                this.number++;
                this.$emit('update:number', this.number);
            }
        },
        mounted() {
 
        }
    }
</script>
 
<style lang="scss" scoped>
    .title {
        color: #F0AD4E;
    }
</style>

 

  

posted @   Sunsin  阅读(3018)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示