简体   繁体   English

错误类型错误:_co.getAdmin 不是函数

[英]ERROR TypeError: _co.getAdmin is not a function

This is an Angular application and it's something wrong with function getAdmin() in auth.service , but I have no idea what.这是一个Angular应用程序,它的getAdmin()函数getAdmin()auth.service ,但我不知道是什么。 When I moved this function to app.component.ts and changed to "getAdmin()" in HTML, it was OK, but I need this function in service.当我将此函数移至app.component.ts并更改为 HTML 中的"getAdmin()"时,没问题,但我需要在服务中使用此函数。 Please tell me what's wrong and how can I fix it.请告诉我出了什么问题,我该如何解决。

PS.附注。 variable admin returns 'true' or 'false' as a string.变量admin以字符串形式返回'true''false' It's e-Commerce app with user authentication, token and now I try to add admin.这是带有用户身份验证和令牌的电子商务应用程序,现在我尝试添加管理员。

auth.service.ts: auth.service.ts:

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Router } from '@angular/router';

@Injectable({
  providedIn: 'root'
})
export class AuthService {

 constructor(private http: HttpClient, private _router: Router) { }

getAdmin() {
    if (localStorage.getItem('admin') === 'true') return true;
    return false;
  }
}

app.component.ts: app.component.ts:

import { Component } from '@angular/core';
import { AuthService } from './auth.service';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  constructor(private _authService: AuthService) {
  }
}

app.component.html: app.component.html:

<a class="nav-link" *ngIf="_authService.getAdmin()" routerLink="/admin" routerLinkActive="active">Admin</a>

It's always better to not call a function written in service from html .最好不要从 html 调用在 service 中编写的函数。 I think, you can resolve your current issue by changing your service scope from private to public.我认为,您可以通过将服务范围从私有更改为公共来解决当前的问题。 But the best way is to call a function inside component from html and call the getAdmin() inside service from that function.但最好的方法是从 html 调用组件内部的函数,并从该函数调用内部服务的getAdmin() The best practise is to keep the service scope to private itself while doing dependency injection.最佳实践是在进行依赖注入时将服务范围保持为私有。

app.component.html应用程序组件.html

<a class="nav-link" *ngIf="adminExists()" routerLink="/admin" routerLinkActive="active">Admin</a>

app.service.ts应用服务.ts

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Router } from '@angular/router';

@Injectable({
  providedIn: 'root'
})
export class AuthService {

 constructor(private http: HttpClient, private _router: Router) { }

getAdmin() {
    if (localStorage.getItem('admin') === 'true') return true;
    return false;
  }
}

app.component.ts app.component.ts

import { Component } from '@angular/core';
import { AuthService } from './auth.service';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  constructor(private _authService: AuthService) {
  }
  adminExists(){
         return _authService.getAdmin();
  }
}

Change scope of service private to public in app.component.ts:在 app.component.ts 中将私有服务范围更改为公共服务:

import { Component } from '@angular/core';
import { AuthService } from './auth.service';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  constructor(public _authService: AuthService) {
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM