Angular2将身份验证令牌传递给另一个服务

时间:2018-07-31 18:39:04

标签: angular

我在获取身份验证令牌的代码如下,

@Injectable()
export class AuthenticationService {


   @Input() isValid:boolean=false;

  constructor(private http: HttpClient, private router: Router) { }
  auth_token:string;
  /** sign in user */

  login(username, password) {

      this.req = this.http.post('URL for Authenticaion', {
      "username": username, 
      "password": password 
    })
        .subscribe(
              result => {
               console.log(result);
                this.router.navigate(['dashboard']);




                    }

}

现在,我想将此令牌传递给另一个服务,该服务将检索所有数据。我面临的问题是result.auth_token对于此当前块而言是本地的,因此无法在其外部识别。 如何返回其值?

1 个答案:

答案 0 :(得分:0)

<template lang="pug">
    .modal(v-if="popupShown", style="display:block", v-on:click="hideModalSafe")
        .modal-dialog.modal-lg(v-on:keydown.enter="syncSchedule()")
            .modal-content
                h3.modal-header {{moment(currentSchedule.start).format('YYYY-MM-DD')}}
                    button.close(type="button",v-on:click="popupShown=false") &times;
                .modal-body
                    .row
                        .col.form-group
                            label(for="schedule-start") {{__('Start')}}
                            k-input-time(:date.sync="currentSchedule.start")
                        .col.form-group
                            label(for="schedule-end") {{__('End')}}
                            k-input-time(:date.sync="currentSchedule.end")
                        .col.form-group
                            label(for="schedule-pause") {{__('Pause')}}
                            k-input-minutes(:minutes.sync="currentSchedule.pause")
                    .row
                        .col.form-group
                            label(for="schedule-site-id") {{__('Site')}}
                            k-combobox(model="modSites", context="selection", 
                            :value.sync="currentSiteMod")
                        .col.form-group
                            label(for="schedule-profile-id") {{__('Profile')}}
                            k-combobox(model="modProfiles", context="selection", 
                            :value.sync="currentProfileMod")
                        .col.form-group
                            label(for="schedule-employee-id") {{__('Employee')}}
                            k-combobox(model="modEmployees", context="selection", 
                            :value.sync="currentEmployeeMod")
                .modal-footer
                    .btn.btn-danger.mr-auto(v-on:click="deleteSchedule()")
                        k-icon(icon="delete")     
                        |  {{__('Remove')}}
                    .btn.btn-primary(v-on:click="syncSchedule()", tabindex="0") {{__('Save')}}
</template>


export default {
    ......
    computed:{
        currentSiteMod:{
            get(){
                return this.$store.state.manageSchedulesModale.currentSite;
            },
            set(value){
                this.$store.dispatch("manageSchedulesModale/updateCurrentSite", value);
            }
        },
        currentProfileMod:{
            get(){
                return this.$store.state.manageSchedulesModale.currentProfile;
            },
            set(value){
                this.$store.dispatch("manageSchedulesModale/updateCurrentProfile", value);
            }
        },
        currentEmployeeMod: {
            get(){
                return this.$store.state.manageSchedulesModale.currentEmployee;
            },
            set(value){
                this.$store.dispatch("manageSchedulesModale/updateCurrentEmployee", value);
            }
        }
    }
}