javascript - 找不到名称 'ENV' returnTo : ENV. BASE_URI

标签 javascript node.js angular auth0

我假设我的身份验证服务无法加载配置文件,从而导致错误。 我正在关注 AUTH0 上的教程。这是链接 https://auth0.com/blog/real-world-angular-series-part-2/ 。 我必须从 env.config.ts 导入一个文件,该文件将由我的身份验证服务使用。以下是代码 env.config.ts

const _isDev = window.location.port.indexOf('4200') > -1;
const getHost = () => {
  const protocol = window.location.protocol;
  const host = window.location.host;
  return `${protocol}//${host}`;
};
const apiURI = _isDev ? 'http://localhost:8083/api/' : `/api/`;

export const ENV = {
  BASE_URI: getHost(),
  BASE_API: apiURI
};

这里引用了教程“此代码检测主机环境并设置应用程序的基本 URI 和基本 API URI。我们将在需要检测和使用这些 URI 的任何地方导入此 ENV 配置。” 这个env文件被导入到auth0需要的文件中, 这是我的 auth.config.ts

片段
import { ENV } from './../core/env.config';

interface AuthConfig {
  CLIENT_ID: string;
  CLIENT_DOMAIN: string;
  AUDIENCE: string;
  REDIRECT: string;
  SCOPE: string;
  NAMESPACE: string;
};

export const AUTH_CONFIG: AuthConfig = {
  CLIENT_ID: '[xxx]',
  CLIENT_DOMAIN: '[]', // e.g., kmaida.auth0.com
  AUDIENCE: '[http://localhost:8083/api/]', // e.g., http://localhost:8083/api/
  REDIRECT: `${ENV.BASE_URI}/callback`,
  SCOPE: 'openid profile email',
  NAMESPACE: 'http://myapp.com/roles'
};


请看一下教程,也许可以分享我可能错过的内容。

auth.service.ts 使用配置文件本身

   // Remove data from localStorage
    this._clearExpiration();
    this._clearRedirect();
    // End Auth0 authentication session
    this._auth0.logout({
      clientId: AUTH_CONFIG.CLIENT_ID,
      returnTo: ENV.BASE_URI
    });
  }

这就是我的 auth.service.ts 导入部分的样子

import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { BehaviorSubject, Subscription, of, timer } from 'rxjs';
import { mergeMap } from 'rxjs/operators';
import { AUTH_CONFIG } from './auth.config';
import * as auth0 from 'auth0-js';
import { ENV } from './../core/env.config';

我的 cmd 中发出的错误读取

ERROR in src/app/auth/auth.service.ts:92:17 - error TS2304: Cannot find name >'ENV'.

92 returnTo: ENV.BASE_URI. I also suspect the way the imports are handled from core have a problem ,as i really im not well vast with the slash imports. I have my app directory that holds the auth and core folder as direct children.

最佳答案

尝试在构造函数内声明 ENV。

public constructor(){
const env = ENV
}

然后使用它。

console.log(env.BASE_URI)

关于javascript - 找不到名称 'ENV' returnTo : ENV. BASE_URI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57026788/

相关文章:

javascript - 如何修复在 Angular 4 中找不到模块 'typescript'?

asynchronous - Angular 2 : Async pipe for array index

css - 从组件方法中将样式应用于 Angular Toast

javascript - 对象函数没有方法(我不是 javascript 编码器)

javascript - 使用外部值更新 Angular react 形式模型

javascript - 如何使用 javascript 或 jquery 从字符串中提取 html 标签的内容?

javascript - 在入口点自动加载 block

node.js - Strongloop Oracle 通过脚本连接

node.js - Node 应用程序在本地主机上运行,​​而不是在服务器上运行(Digitalocean、express.js)

javascript - 如何在 Angular 2 中注入(inject) document.body