vuejs2 - 在调整大小事件上更新 Vue 模板
问题描述
我有一个组件,我在其中创建了一个包含其他数组的数组:
mounted() {
this.populateTrackedMoviesState();
window.addEventListener('resize', this.MoviesInRows);
return () => {
window.removeEventListener('resize', this.MoviesInRows);
};
}
MoviesInRows() {
const numberOfMovies = Math.floor((window.innerWidth - 24) / 185);
console.log(chunk(TrackedMoviestStore.state.trackedMovieList, numberOfMovies).length);
return chunk(TrackedMoviestStore.state.trackedMovieList, numberOfMovies);
}
这里发生的重要事情是,当我调整显示console.log
中MoviesInRows
的正确值时。
在我的模板中,我根据以下MoviesInRows()
方法呈现电影列表:
<ul id="movieList" v-for='(movieRow, index) in MoviesInRows()' :key='index'>
<li
v-for='movie in movieRow'
:key='movie.id'>
<MovieInOverview :movie=movie />
{{ MoviesInRows().length }}
</li>
</ul>
{{ MoviesInRows().length }}
永远不会改变,尽管在console.log
显示MoviesInRows
返回值应该改变。
解决方案
我不完全确定这是如何解决问题的,但是:
numberOfMovies = 0;
MoviesInRows() {
this.numberOfMovies = Math.floor((window.innerWidth - 24) / 185);
return chunk(TrackedMoviestStore.state.trackedMovieList, this.numberOfMovies);
}
现在,当我调整屏幕大小时,模板会正确更新。
推荐阅读
- c++ - Qt/QML/OpenGL:Z-ordering 不适用于 QML 下带有 OpenGL 的半透明项目
- r - 仅通过第一个单词查找两个数据帧之间的匹配行
- javascript - JavaScript 类函数可以添加到 ul,但要清除时未定义
- python - 以下函数在打印我正在寻找的结果时返回“none”。为什么是这样?
- django - Django 和汇总孙子记录
- typescript - 打字稿文件命名约定
- sql-server - 具有可选输出的存储过程
- spring - 在 AWS Lambda 中运行时,Dagger 与 Guice 或 Spring 相比要快多少?
- swift - 以编程方式添加标签栏标题和图标 Swift
- javascript - 如何确保功能与承诺继续执行