• vue2.0 vue.set()


    <!DOCTYPE html>
    <html lang="en">
        <head>
            <meta charset="UTF-8">
            <script type="text/javascript" src="vue2.2.js"></script>
            <title>Vue.set 全局操作</title>
        </head>
        <body>
            <h1>Vue.set 全局操作</h1>
            <div id="app">
                <ul>
                    <li v-for=" item in arr">{{item}}</li>
                </ul>
            </div>
            <button onclick="add()">外部添加</button>
            <script type="text/javascript">
                function add() {
                    console.log("我已经执行了");
                    //vm.arr[1] = 'd'; 用数组下标监测不到数据更新
                    Vue.set(vm.arr,1,'d');
                }
                var outData = {
                    arr: ['a', 'b', 'c']
                };
                var vm = new Vue({
                    el: '#app',
                    data: outData
                })
            </script>
        </body>
    </html>

    外部数据,就是不在Vue构造器里里的data处声明,而是在构造器外部声明,然后在data处引用就可以了。外部数据的加入让程序更加灵活,我们可以在外部获取任何想要的数据形式,然后让data引用。

    在外部改变数据的三种方法:

    1、用Vue.set改变

    function add(){
      Vue.set(outData,'count',2);
    }
    2、用Vue对象的方法添加

    app.count++;

    3、直接操作外部数据

    outData.count++;

  • 相关阅读:
    507.Perfect Number
    441.Arranging Coins
    344.Reverse String
    160.Intersection of Two Linked Lists
    HDU-2521 反素数
    HDU-2710 Max Factor
    HDU-2552 三足鼎立
    HDU-2549 壮志难酬
    HDU-2548 两军交锋
    HDU-2550 百步穿杨
  • 原文地址:https://www.cnblogs.com/lhl66/p/7492553.html
Copyright © 2020-2023  润新知