Vue.set 用于操作数组,由于javascript的限制,无法直接使用下标对数组进行直接操作,
所以vue提供Vue.set这种方法来操作数组,代码示例如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>vue.set 全局操作</title>
<script src="../assets/js/vue.js"></script>
</head>
<body>
<h1>vue.set 全局操作</h1>
<hr>
<div id="app">
<ul>
<li v-for="(item,index) in arr">
{{item}}
</li>
</ul>
</div>
<p><button onclick="add()">add</button></p>
<script>
function add() {
Vue.set(app.arr, 1, 'dd')
}
var outData = {
arr: ['aa', 'bb', 'cc']
}
var app = new Vue({
el: '#app',
data: outData
})
</script>
</body>
</html>