vue.js - 如何在 Vue 应用程序中的子节点之前执行父生命周期挂钩?
问题描述
我们有以下父组件 ( App.vue
)
<template>
<b-container
id="app"
fluid
class="px-0"
>
<b-row no-gutters>
<side-bar :routes="routes"/>
<b-col>
<notifications position='top center' />
<user-info :routes="routes"/>
<b-row no-gutters class="router-view">
<b-col>
<router-view/>
</b-col>
</b-row>
</b-col>
</b-row>
</b-container>
</template>
<script>
import UserInfo from './views/navbar/UserInfo'
import SideBar from './views/sidebar/SideBar'
import AuthService from '@/services/auth.service'
import NotificationsService from '@/services/notifications.service'
export default {
name: 'App',
components: { UserInfo, SideBar },
data () {
return {
routes: [
{ name: 'app.dashboard', params: {}, icon: 'home', text: 'Dashboard' },
{ name: 'app.jobs', params: {}, icon: 'briefcase', text: 'Jobs' },
{ name: 'app.firms', params: {}, icon: 'th-large', text: 'Firms' }
// { name: '#', params: {}, icon: 'user', text: 'People' },
// { name: '#', params: {}, icon: 'gavel', text: 'Bids' },
// { name: '#', params: {}, icon: 'users', text: 'MF People' },
// { name: '#', params: {}, icon: 'chart-bar', text: 'Reports' }
]
}
},
created () {
this.loginOnLoad()
},
methods: {
async loginOnLoad () {
this.authService = new AuthService()
let user = this.authService.sso()
if (!user) {
this.loginFailed = true
await this.login()
await this.authService.assignAccessTokenToHeader()
NotificationsService.successNotification('Logged in successfully')
} else {
await this.authService.assignAccessTokenToHeader()
NotificationsService.successNotification('Welcome Back')
}
}
}
}
</script>
<style>
</style>
assignAccessTokenToHeader
调度一个 Vuex 操作,该操作分配商店中经过身份验证的用户的结果
这是子组件 ( Dashboard.vue
)
<template>
<div>
<b-row class="border-bottom" no-gutters>
<b-col sm="12" class="px-4 py-3">
<h3 class="text-uppercase">Dashboard</h3>
<h5 class="font-weight-normal text-secondary mb-0">Personalised Favourites</h5>
</b-col>
</b-row>
<b-row no-gutters>
<b-col>
<b-row no-gutters>
<b-col sm="6">
<b-card title="Firms" class=""></b-card>
</b-col>
</b-row>
<b-row no-gutters>
<b-col sm="6">
<b-card title="Firms" class=""></b-card>
</b-col>
</b-row>
<b-row no-gutters>
<b-col>
<b-card title="Firms" class=""></b-card>
</b-col>
</b-row>
<b-row no-gutters>
<b-col>
<b-card title="Firms" class=""></b-card>
</b-col>
</b-row>
<!-- <b-card title="Bids" class="d-inline-flex w-25">-->
<!-- </b-card>-->
<!-- <b-card title="Jobs" class="d-inline-flex w-50">-->
<!-- </b-card>-->
<!-- <b-card title="People" class="d-inline-flex w-50">-->
<!-- </b-card>-->
</b-col>
</b-row>
</div>
</template>
<script>
import AppService from '@/services/app.service'
import { mapState } from 'vuex'
export default {
name: 'Home',
created () {
this.loadGridAndContent(this.userDetails.psref)
},
computed: {
...mapState({
userDetails: state => state.auth.user
})
},
methods: {
async loadGridAndContent (psRef) {
let data = await AppService.loadDashboard(psRef)
console.log(data)
}
}
}
</script>
<style scoped>
</style>
Dashboard.vue
router-view
在of内部呈现,App.vue
并且路线是/
现在,我们希望App.vue
创建的钩子总是首先运行,但它看起来Dashboard.vue
试图在它之前运行其创建的钩子App.vue
,因此失败,因为它使用psref
的是在父级中获取的。
什么是解决这个问题的方法或正确的实现方法,以便始终首先获取用户登录信息,存储在 Vuex 中,然后子组件可以使用它?
解决方案
通常,每当您的子组件运行取决于父操作或已安装生命周期钩子的mount()时,您可以通过在子组件上放置v-if来避免它预先加载。一旦你的父母完成了它的任务,你只需将一个变量切换为true,然后你的孩子就会被加载,这样你就可以避免空值或空值。
推荐阅读
- python - 使用 MetPy 生成 Countour 图的问题
- c++ - 我将如何添加或乘以极大的数字,例如 20 位数字 x 5 位数字......?
- arrays - 如何从带有注释的展平地图中获取嵌套数组
- java - 如何使用 Selenium 从 URL 中提取特定格式的日期?
- ruby-on-rails-6 - Rails 6 复制及应用,webpacker 的影响
- php - preg_match php 带括号和确定的字符串不起作用
- android - 运行应用程序使用 firebase ml 套件时模块错误中发现重复类
- json - 如何获取特定的 JSON 项目?
- html - 水平滚动书签
- javascript - 无法使用 aws-sdk 获取 AWS 机密 - 我要么丢失凭证错误和一个对象