vue.js - force component to rebuild on route change or vuex state change
问题描述
I want to get into VueJs / Vuex and created a small todo app. There are three links for "all todos", "pending ones" and "completed ones". It's the same route but with a different filter
query.
When changing the route the component will not update, because it seems that updated query is not forcing an component update. But the computed
event gets triggered.
I created a small example showing my current problem
https://codesandbox.io/s/6zx2p0m20r
If you click around on the todo links there will be no component update. If you head over to "another route" and head back, the component was updated (because of a completely different route).
How can I force to update the component on a query update within the same base route?
解决方案
You can add the beforeRouteUpdate
navigation guard to reinitialize your data:
beforeRouteUpdate
is called when the route that renders this component has changed, but this component is reused in the new route.For example, for a route with dynamic params
/foo/:id
, when we navigate between/foo/1
and/foo/2
, the sameFoo
component instance will be reused, and this hook will be called when that happens.(source)
beforeRouteUpdate(to, from, next) {
this.currentTodoFilter = to.query.filter
next()
}
You can also remove your updateTodoFilter
method this way.
推荐阅读
- java - 为什么我的对象不强制转换为不调用默认方法的接口?
- apache-spark - IntelliJ 中的独立 Spark 应用程序
- mysql - 使用下几行的最小值更新行列
- google-kubernetes-engine - 在集群中创建WINDOWS_SAC节点池时如何修复“404 Not Found”错误(快速发布)
- linux - 文件大小没有按预期增长
- android - 在 Android 应用程序和服务器 Couchbase Lite 之间同步数据
- authentication - .NET Core 3(预览版)Web API 返回 UnAuthorized (401) 而不是 NotFound (404)
- angular - 从变量应用类
- java - 合并对象列表的 k 个列表
- javascript - 如何重用常用类型