vue.js:路由保护等待异步值

时间:2017-08-03 08:58:38

标签: vue.js state vue-router vuex

在每个应用程序中,我都有几条路线。例如([{ path: '/reporting', name: 'Reporting', component: reporting, meta: { adminOnly: true } }, ...] 摘录):

reporting

正如您在路径定义中所看到的,当访问if (to.matched.some(route => route.meta.adminOnly)) { if (store.getters.userInfo.isAdmin) { next() } else { next('/') } } else { next() } 时,用户需要具有管理员权限,这是vuex商店中的属性。问题:这个属性是异步的,当初访问守卫时当然是假的。我怎样才能让我的警惕等待它?

后卫:

 Function percentage(f, s, t)
 Dim id, l, k, y
 k = 2
 id = f
 l = s
 y = t
        If id <> 0 Then

           While IsEmpty(Sheet7.Cells(k, 1).Value) = False

               If Sheet7.Cells(k, 1).Value = id Then
                   id = Sheet7.Cells(k, 11).Value
                      If l = 6 Then
                        Sheet7.Cells(k, l).Value = 0.03 * Sheet7.Cells(y, 3).Value
                         l = l + 1
                      ElseIf l = 7 Then
                        Sheet7.Cells(k, l).Value = 0.02 * Sheet7.Cells(y, 3).Value
                         l = l + 1
                       ElseIf l = 8 Then
                        Sheet7.Cells(k, l).Value = 0.01 * Sheet7.Cells(y, 3).Value
                       End If
                End If
                k = k + 1
                Wend
              percentage = percentage(id, l, y)
        End If

  End Function

1 个答案:

答案 0 :(得分:2)

使用store.watch观看吸气剂怎么样?

假设如果尚未提取数据,则getter返回null

if (store.getters.userInfo.isAdmin === null) {
    const watcher = store.watch(store.getters.userInfo.isAdmin, isAdmin => {
        watcher(); // stop watching
        if (isAdmin) next();
        else next('/');
    });
}
else if (store.getters.userInfo.isAdmin) next();
else next('/');