Xizmatda xizmatga kirishingiz kerak

restangularInit funktsiyasiga (mening app.module.ts) kirishim kerak bo'lgan (AuthService) xizmatim bor, lekin qanday qilib men buni bila olmayman.

Uni AppModule sinfiga ko'chirishga harakat qildim, ammo keyinchalik kechiktiriladi.

Masalan, xizmat vazifalarini chaqirish. getToken kutilmoqda kabi ishlaydi.

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { MaterialModule } from '@angular/material';
import { FlexLayoutModule } from "@angular/flex-layout";
import { RestangularModule } from 'ng2-restangular';

// Components
import { AppComponent } from './app.component';
import { ProductComponent } from './components/product/product.component';

// Services
import { AuthService } from './services/auth.service';

export function restangularInit(RestangularProvider, AuthService) {
  console.log(AuthService);//this is undefined
  let token = AuthService.getToken(); //This is what I want to do
  RestangularProvider.setBaseUrl('api');
  RestangularProvider.setDefaultHeaders(
    {'Authorization': 'Bearer ' + token},
  );
}

@NgModule({
  declarations: [
    AppComponent,
    ProductComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    MaterialModule,
    FlexLayoutModule,
    RestangularModule.forRoot(restangularInit)
  ],
  providers: [
    AuthService
  ],
  entryComponents: [ProductComponent],
  bootstrap: [AppComponent]
})
export class AppModule { }
4

6 javoblar

Statik usulni getToken() yarating, keyin uni kodingizda qilgan kabi kirishingiz mumkin.

let token = AuthService.getToken(); //This is what I want to do
1
qo'shib qo'ydi
Muammo shundaki, xizmat ko'rsatgichidan localforage dan keladigan so'zni ishlatadi. shuning uchun so'rovlar token belgilanmasdan oldin yuboriladi :(
qo'shib qo'ydi muallif Mackelito, manba

Statik usulni getToken() yarating, keyin uni kodingizda qilgan kabi kirishingiz mumkin.

let token = AuthService.getToken(); //This is what I want to do
1
qo'shib qo'ydi
Muammo shundaki, xizmat ko'rsatgichidan localforage dan keladigan so'zni ishlatadi. shuning uchun so'rovlar token belgilanmasdan oldin yuboriladi :(
qo'shib qo'ydi muallif Mackelito, manba

Statik usulni getToken() yarating, keyin uni kodingizda qilgan kabi kirishingiz mumkin.

let token = AuthService.getToken(); //This is what I want to do
1
qo'shib qo'ydi
Muammo shundaki, xizmat ko'rsatgichidan localforage dan keladigan so'zni ishlatadi. shuning uchun so'rovlar token belgilanmasdan oldin yuboriladi :(
qo'shib qo'ydi muallif Mackelito, manba

hujjatiga muvofiq buni quyidagi kabi qilishingiz mumkin:

RestangularModule.forRoot([AuthService], restangularInit)

undan keyin

export function restangularInit(RestangularProvider, authService: AuthService) {
  console.log(authService);//this is AuthService instance
  let token = authService.getToken(); //This is what I want to do
  RestangularProvider.setBaseUrl('api');
  RestangularProvider.setDefaultHeaders(
    {'Authorization': 'Bearer ' + token},
  );
}
1
qo'shib qo'ydi
Muammo shundaki, xizmat ko'rsatgichidan localforage dan keladigan so'zni ishlatadi. shuning uchun so'rovlar token belgilanmasdan oldin yuboriladi :(
qo'shib qo'ydi muallif Mackelito, manba
Ilova ochilganda AuthService localforage.getItem ("auth") ni ishga tushiramiz, keyin ((auth) => {bu erda belgini olib tashlash}); "lekin agar serverdan javob sekin bo'lsa, komponentlar va'da qilingan.
qo'shib qo'ydi muallif Mackelito, manba
Nima uchun oldinroq token olmaysiz?
qo'shib qo'ydi muallif yurzui, manba
APP_INITIALIZER, canActivate, resolver kabi amallarni bajarish uchun ko'p variant mavjud
qo'shib qo'ydi muallif yurzui, manba

hujjatiga muvofiq buni quyidagi kabi qilishingiz mumkin:

RestangularModule.forRoot([AuthService], restangularInit)

undan keyin

export function restangularInit(RestangularProvider, authService: AuthService) {
  console.log(authService);//this is AuthService instance
  let token = authService.getToken(); //This is what I want to do
  RestangularProvider.setBaseUrl('api');
  RestangularProvider.setDefaultHeaders(
    {'Authorization': 'Bearer ' + token},
  );
}
1
qo'shib qo'ydi
Muammo shundaki, xizmat ko'rsatgichidan localforage dan keladigan so'zni ishlatadi. shuning uchun so'rovlar token belgilanmasdan oldin yuboriladi :(
qo'shib qo'ydi muallif Mackelito, manba
Ilova ochilganda AuthService localforage.getItem ("auth") ni ishga tushiramiz, keyin ((auth) => {bu erda belgini olib tashlash}); "lekin agar serverdan javob sekin bo'lsa, komponentlar va'da qilingan.
qo'shib qo'ydi muallif Mackelito, manba
Nima uchun oldinroq token olmaysiz?
qo'shib qo'ydi muallif yurzui, manba
APP_INITIALIZER, canActivate, resolver kabi amallarni bajarish uchun ko'p variant mavjud
qo'shib qo'ydi muallif yurzui, manba

hujjatiga muvofiq buni quyidagi kabi qilishingiz mumkin:

RestangularModule.forRoot([AuthService], restangularInit)

undan keyin

export function restangularInit(RestangularProvider, authService: AuthService) {
  console.log(authService);//this is AuthService instance
  let token = authService.getToken(); //This is what I want to do
  RestangularProvider.setBaseUrl('api');
  RestangularProvider.setDefaultHeaders(
    {'Authorization': 'Bearer ' + token},
  );
}
1
qo'shib qo'ydi
Muammo shundaki, xizmat ko'rsatgichidan localforage dan keladigan so'zni ishlatadi. shuning uchun so'rovlar token belgilanmasdan oldin yuboriladi :(
qo'shib qo'ydi muallif Mackelito, manba
Ilova ochilganda AuthService localforage.getItem ("auth") ni ishga tushiramiz, keyin ((auth) => {bu erda belgini olib tashlash}); "lekin agar serverdan javob sekin bo'lsa, komponentlar va'da qilingan.
qo'shib qo'ydi muallif Mackelito, manba
Nima uchun oldinroq token olmaysiz?
qo'shib qo'ydi muallif yurzui, manba
APP_INITIALIZER, canActivate, resolver kabi amallarni bajarish uchun ko'p variant mavjud
qo'shib qo'ydi muallif yurzui, manba
Angular Uzbekistan
Angular Uzbekistan
107 ishtirokchilar

Guruh Angular bo'yicha muloqot qilish uchun ochilgan Sizni qiziqtirsa: @nodejs_uz @react_uz @angular_uz @yiiframework_uz @js_uzb @typescript_uzb @vuejs_uz @ngTashkent @laravel_uz @uzdevgroup Ish o'rinlari @UzDev_jobs

ngTashkent
ngTashkent
77 ishtirokchilar

Правила просты: 1. Не хамить 2. Не хейтить 3. Реакт не нужен Ссылки: https://t.me/angular_ru - старший брат https://t.me/angular_uz - ангулар на узбекском @js_uzb @vuejs_uz @react_uz @nodejs_uz @yiiframework_uz @uzdevgroup @UzDev_Jobs @tasdev_talks