I get this error when I call my API_URL that I defined in my enviroment.ts file in a service class.
当我在服务类中调用在我的enviroment.ts文件中定义的API_URL时,我收到这个错误。
Error: src/app/product/product.service.ts:12:25 - error TS2339: Property 'API_BASE_URL' does not exist on type '{}'.
错误:SRC/APP/PRODUCT/Product.Serice.ts:12:25-错误TS2339:类型‘{}’上不存在属性‘API_BASE_URL’。
12 baseUrl = environment.API_BASE_URL;
12base Url=环境.API_BASE_URL;
**enviroment.ts
**
**enviroment.ts**
export const environment = {
production :false,
API_BASE_URL : 'http://localhost:8092'
}
**enviroment.development.ts
**
**enviroment.Development.ts**
export const environment = {};
**product.service.ts
**
**Product.Serice.ts**
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Product } from '../product';
import { environment } from 'src/environments/environment';
@Injectable({
providedIn: 'root'
})
export class ProductService {
constructor(private http: HttpClient) { }
baseUrl = environment.API_BASE_URL;
createProduct(product: Product){
return this.http.post(this.baseUrl, product);
}
getProductList(){
return this.http.get(this.baseUrl);
}
}
I think I showed the URL correctly. It was actually running before I made changes to the service class. But now it throws errors. How can i solve it. Should I take the URL elsewhere or specify a type?
我想我显示的URL是正确的。在我更改服务类之前,它实际上是在运行的。但现在它抛出了错误。我怎么才能解决这个问题。我应该在其他地方使用URL还是指定类型?
更多回答
The answer seems to hide in your question. Is it possible that you're using the development mode? You should put a value for the API_BASE_URL
in the environment.development.ts
file as well.
答案似乎隐藏在你的问题中。您是否可能正在使用开发模式?您还应该将API_BASE_URL的值放在环境.Development ment.ts文件中。
优秀答案推荐
first you can try define API_BASE_URL in your enviroment.development.ts file as an empty string. Like this:
首先,您可以尝试将enviroment.Development.ts文件中的API_BASE_URL定义为空字符串。就像这样:
API_BASE_URL : ''
The other thing i notice, i dont know if is only an error in the post is that you put "enviroment.ts" ... and it should be "enviroNment.ts, with an "N" letter between o and m letters.
我注意到的另一件事,我不知道是否只是一个错误的职位是,你把“environment.ts”.它应该是“enviroNment.ts,在o和m之间有一个“N”字母。
更多回答
I misspelled the file names while writing :) By the way, your advice worked. Thank you
我写的时候把文件名拼错了:)顺便说一句,你的建议奏效了。谢谢
im glad your problem got resolved, one more thing: wy do you use "environment.development.ts"? i mean, angular read environment.ts as a dev for default. And environment.prod.ts for production when you use --prod mode.
我很高兴你的问题得到了解决,还有一件事:你为什么使用“Environmental.Development ment.ts”?我的意思是,角度读取环境.ts作为默认的开发工具。当您使用--prod模式时,用于生产的环境为.prod.ts。
我是一名优秀的程序员,十分优秀!