【问题标题】:How do you fix "Property ' ' does not exist on type 'typeof import" error?你如何解决“属性''不存在类型'typeof import”错误?
【发布时间】:2021-12-05 02:05:56
【问题描述】:

致力于身份验证的 Aungluar 新手。我将 auth0-angular 包下载到我的项目中,现在我在创建 WebAuth() 实例时遇到此错误(IDE:VS Code):

" 属性 'WebAuth' 不存在于类型 'typeof import("c:/Users/Owner/Desktop/coding_course_docs/AngularAudioPlayer/node_modules/@auth0/auth0-angular/auth0-auth0-angular")' "

这是我的代码:

import { Injectable } from '@angular/core';

import * as auth0 from '@auth0/auth0-angular';

import { environment } from '../../environments/environment';



import{

  Observable,
  BehaviorSubject,
  bindNodeCallback, 
  of
} from 'rxjs';

import { Router } from '@angular/router';


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

  //instance of auth0-WebAuth that is used for authentication
  auth0 = new auth0.WebAuth({

    clientID: environment.auth0.clientID,

    domain: environment.auth0.domain,

    responseType: 'token id_token',

    scope: 'openid profile email'

  });

  //'localStorage' keys (for storing authentication and user profile data) that track whether or not to renew token
  private _authFlag = 'isLoggedIn';
  
  private _userProfileFlag = 'userProfile';

对我做错了什么有任何想法吗?

【问题讨论】:

    标签: angular import auth0 webauthn


    【解决方案1】:

    您是否尝试过对 auth0 使用另一个导入? import * as auth0 from "auth0-js";

    请注意,使用它需要以下依赖项:auth0-js,而不是 @types/auth0-js(也应该添加,但它只是添加,以便您在 TypeScript 中为 auth0 获得更好的类型)

    如果你想使用auth0-angular,那么你应该切换到另一个导入类型:import { AuthService } from '@auth0/auth0-angular';。您可以在此处找到有关此 AuthService 使用的更多详细信息:https://auth0.com/docs/libraries/auth0-angular-spa

    虽然第一个应该可以解决您的问题,但从长远来看,第二个可能是更好的方法。

    【讨论】:

    • 在此之前我试过import * as auth0 from "auth0-js";,CLI一直给我一个错误,我实际上是为那个here发布的
    • 另外,我应该尝试将 AuthService 导入此组件或 app.module 吗?
    • 你是否从 NPM 安装了 auth0-js 包?通过查看您在您提到的另一个问题中发布的 package.json,我只能看到 @types/auth0-js 但此包仅包含 TypeScript 的类型定义,而不包含 auth0 本身。尝试在 package.json 中添加 auth0-js 依赖项并运行 npm install 并检查它是否有帮助(并将此导入 import * as auth0 from "auth0-js"; 与此包一起使用)
    • 关于 AuthService - 如果您决定要使用此服务而不是 auth0-js,请仅使用此服务。通过这种方式检查我上面提供的链接,有一个带有解释的示例。基本上你需要导入模块并在 app.module 中配置它(使用 AuthModule.forRoot),然后将其注入组件的构造函数并使用注入的实例。
    • Wooooow 成功了!非常感谢!我没有意识到auth0-js@types/auth0-js 之间有区别。请原谅我的新手!非常感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-13
    • 2021-04-22
    • 2020-09-19
    • 2021-01-09
    • 2018-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多