【问题标题】:api-key not found in browser (404)在浏览器中找不到 api-key (404)
【发布时间】:2019-05-13 09:40:33
【问题描述】:

我正在使用 angular 和 ionic4 创建一个新闻应用程序。我已将我的 api url 和 api 密钥放在 environment.ts 文件中,如下所示:

export const environment = {
  production: false,
  apiUrl:'https://n------',
  apiKey:'api-key----'
};

我的 news.service.ts 文件如下:

import { Injectable } from '@angular/core';
    import {HttpClient} from '@angular/common/http';
    import {environment} from '../environments/environment';
    
    const API_URL = environment.apiUrl;
    const API_KEY = environment.apiKey;
    
    @Injectable({
      providedIn: 'root'
    })
    export class NewsService {
    
      constructor(private http:HttpClient) { }
    
    
      getData(url){
        return this.http.get('${API_URL}/${url}&apiKey=${API_KEY}');
      }
    }

但是,当我刷新浏览器时,它显示以下错误:

【问题讨论】:

  • 解决了您的问题吗?如果答案有帮助,请采纳:)

标签: angular api ionic-framework hybrid-mobile-app


【解决方案1】:

我认为您要做的是在传递给 http.get 的字符串中插入变量。为此,您必须使用反引号(例如:`)而不是刻度。试试看,然后告诉我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-02
    • 2011-02-22
    • 1970-01-01
    • 2021-02-18
    • 2020-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多