javascript - 模板中的 Vue.js 动态数组不起作用
问题描述
我刚开始摆弄 VueJs,我尝试了一个简单的例子,其中一个数组有值并在模板中使用它,它工作得很好
<body>
<div id="app">
{{operations.join(', ')}}
</div>
</body>
<a href=""></a>
<script>
var app = new Vue({
el: '#app',
data: {
message: 'Understanding Hooks',
operations: ['One', 'Two'],
}
})
</script>
但是,当我尝试动态填充操作(数组)时,页面/浏览器变得无响应(以下是代码)。任何输入都会有所帮助。
<script>
var app = new Vue({
el: '#app',
data: {
message: 'Undstanding Hooks',
operations: ['One', 'Two'],
},
created: function() {
console.log('=> ', this.operations)
this.operations.push('CREATED : ');
},
mounted: function() {
this.operations.push('MOUNTED : ');
},
updated: function() {
this.operations.push('UPDATED : ');
},
destroyed: function() {
this.operations.push('DESTROYED : ');
},
})
</script>
解决方案
更新
this.operations.push('UPDATED : ');
将使组件updated
挂钩无限运行。请尝试删除它。
还有一个关于 immutable 和 reactive 的最佳实践。的参考this.operations
没有改变。
你应该使用
this.operations = [...this.operations, 'CREATED : ']
代替
this.operations.push('CREATED : ');
或者
this.operations = this.operations.concat(['CREATED :'])
推荐阅读
- reactjs - 在反应应用程序中保护 Firebase API 密钥以使其无法公开访问的最佳方法是什么?
- php - Mysql GROUP BY 和 Union 基于日期条件
- c++ - 带有 std::function 的 RAII
- c# - 如何在 WPF 后面的代码中绑定属性
- javascript - 如何在选择之前禁用按钮?
- c# - Docker容器环境变量中的.Net Core 3.1应用程序不起作用
- c# - Google Drive API 在本地工作,但在部署时不工作
- android - 如何将水平recyclerview扩展到垂直
- sql - 如何合并作为 Postgres 中 Concact 字符串一部分的空/空值?
- ruby-on-rails - Rails 协会返回连接模型而不是实际模型