Skip to content

Commit 032b4ab

Browse files
committed
以计算属性代替 watcher
1 parent c0181b5 commit 032b4ab

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

src/v2/guide/migration-vue-router.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -244,17 +244,17 @@ if (route.meta.requiresAuth) {
244244

245245
由于 vue-router 将 `?users=Tom` 处理为 `query.users == 'Tom'`,除非你确信数组中的元素将超过一个,你需要检查并在必要时转换该 query 为 数组。
246246

247-
一个可行的方案是添加如下类型的 watcher:
247+
一个可行的方案是添加如下所示的计算属性:
248248

249249
```javascript
250-
'$route.query.users': {
251-
handler(val) {
252-
this.$route.query.users = Array.isArray(val) ? val : [val]
253-
},
254-
immediate: true
250+
users () {
251+
let val = this.$route.query.users
252+
return Array.isArray(val) ? val : [val]
255253
}
256254
```
257255

256+
然后在用到 `this.$router.query.users` 的地方以计算属性 `users` 代替。
257+
258258
## Route 匹配
259259

260260
路由匹配现在使用 [path-to-regexp](https://github.com/pillarjs/path-to-regexp) 这个包,这将会使得工作与之前相比更加灵活。

0 commit comments

Comments
 (0)