• Reactive的使用(reactive 和 shallowReactive使用上区别)


    与Reactive相关可用的包括reactive、shallowReactive,reactive API可以响应式更新视图,shallowReactive浅层更新视图。外加readonly API

    Reactive

    从vue导出,用reative包裹对象{name:大漂亮}

    <template>
        <div>
        {{person.name}}
        div>
    template>
    
    <script setup lang='ts'>
    // 
    import {reactive} from 'vue'
    let person=reactive({
        name:'大漂亮'
    })
    person.name="小漂亮"
    script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    shallowReactive

    视图上只更新浅层的,但是数据会更新

    例1
    <template>
        <div v-for="index in person">
            <div>{{index}}div>
        div>
        <button @click="add">数组加一button>
    template>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    import { shallowReactive } from 'vue'
    let person=shallowReactive<number[]>([])
    const arr=[1,2,3];
    person.push(...arr);
    function add(){
        console.log(person);
        for(let i=0;i<person.length;i++){
            console.log(person[i]++)
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    结果:点击button,person数组里面的值会加一,视图会同步更新

    例2
    <template>
        <div>
          <div>{{ state }}div>
          <button @click="change1">test1button>
          <button @click="change2">test2button>
        div>
      template>
    <script setup lang='ts'>
    // 
    import {reactive} from 'vue'
    //只能对浅层的数据 如果是深层的数据只会改变值 不会改变视图
    import { shallowReactive } from 'vue'
    const obj = {
      a: 1,
      first: {
        b: 2,
        second: {
          c: 3
        }
      }
    }
    const state = shallowReactive(obj)
     
    function change1() {
      state.a = 7
    }
    function change2() {
      state.first.b = 8
      state.first.second.c = 9
      console.log(state);
    }
    
    script>
    
    • 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

    结果:
    页面原始显示
    { “a”: 1, “first”: { “b”: 2, “second”: { “c”: 3 } } }
    点击text1视图结果为:
    { “a”: 7, “first”: { “b”: 2, “second”: { “c”: 3 } } }
    点击text2视图结果为
    { “a”: 7, “first”: { “b”: 2, “second”: { “c”: 3 } } }
    控制台输出为:
    {
    “a”: 7,
    “first”: {
    “b”: 8,
    “second”: {
    “c”: 9
    }
    }
    }
    由此可见,使用shallowReactive深层次数据会更新,但不会更新到视图上。
    如果将代码改为

    <script>
    import { shallowReactive } from 'vue'
    const obj = {
      a: 1,
      first: {
        b: 2,
        second: {
          c: 3
        }
      }
    }
    const state = shallowReactive(obj)
     
    function change1() {
      state.a = 7
    }
    function change2() {
      state.first.b = 8
      state.first.second.c = 9
      console.log(state);
    }
    
    script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    点击test1视图结果为{ “a”: 7, “first”: { “b”: 2, “second”: { “c”: 3 } } }
    点击test2视图结果为{ “a”: 7, “first”: { “b”: 8, “second”: { “c”: 9 } } }
    由此可见,reactive会将视图全部更新。

    readonly使用示例
    import {readonly} from 'vue'
    const person = reactive({count:1})
    const copy = readonly(person)
     
     person.count++
     
    // copy.count++  直接报错
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    是能显示到视图上,不能做更改。

  • 相关阅读:
    ubuntu服务器中运行flask
    乌班图20.04简易部署k8s+kuboard第三方面板
    浪潮信息面向全行业公布设计指南,以开放规范促进生成式AI多元算力发展
    网络SDK套件:Rebex Total Pack 6.8.0.X FOR NET Crack
    [Linux系统编程]_多线程(四)
    qml中,实时改变TextField中的内容
    Tomcat7集成链路追踪SkyWalking6.6版本
    1107 老鼠爱大米分数 20
    vue的双向绑定Object.definedProperty(obj,key,{set,get})
    测试左移:传统测试方式该如何过渡
  • 原文地址:https://blog.csdn.net/weixin_42491648/article/details/128046132