学习-Vue2-Vue实例-数据与方法-Object.freeze()

Object.freeze(),会阻止修改现有的property,意味着响应系统无法再追踪变化

代码示例:

<!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>
</head>
<body>
    <div id="app">
        <p>{{ foo }}</p>
        <button @click="foo = 'xxx'">change it</button>
        
    </div>
    
</body>
<!-- 单独引入,不可在script间写逻辑代码 -->
<script src="https://cdn.jsdelivr.net/npm/vue@2"></script>

<script>
    var obj = {foo: 'bar111'}
    // Object.freeze(),会阻止修改现有的property,意味着响应系统无法再追踪变化
    Object.freeze(obj)
    var vm = new Vue({
        el: '#app',
        data: obj
    })
</script>
</html>

视图与控制台打印图示:

 

posted on 2023-01-10 16:37  法老的微笑  阅读(100)  评论(0)    收藏  举报