码迷,mamicode.com
首页 > 其他好文 > 详细

vue2.0 vue.set()

时间:2017-09-08 01:15:51      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:执行   click   type   添加   console   rip   data   style   方法   

<!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++;

vue2.0 vue.set()

标签:执行   click   type   添加   console   rip   data   style   方法   

原文地址:http://www.cnblogs.com/lhl66/p/7492553.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!