Angular解析本地json文件

说明:

效果图:

step1:E:\projectgood\ajnine\untitled4\public\config\config.json

bash 复制代码
{
    "calcServerURL": "http://localhost:3000"
}

step2:E:\projectgood\ajnine\untitled4\src\app\orance\orance.component.ts

typescript 复制代码
import {Component, OnInit} from '@angular/core';
import axios from "axios";

@Component({
  selector: 'app-orance',
  standalone: true,
  imports: [],
  templateUrl: './orance.component.html',
  styleUrl: './orance.component.css'
})
export class OranceComponent implements OnInit{

  ngOnInit(): void {
    var name="zhangfeo"
    console.log(name)

    getRuntimeConfig().then(resule=>{
      console.log(resule)
      console.log(resule.calcServerURL)
    })
  }
}

const getRuntimeConfig = async () => {
  const res = await axios.get("/config/config.json");
  const { data } = await res;
  return data;
};

step3:E:\projectgood\ajnine\untitled4\package.json

bash 复制代码
 "dependencies": {
    "@angular/animations": "^18.2.0",
    "@angular/material": "^18.2.10",
    "axios": "^0.19.0",
  },

end

相关推荐
云浪2 分钟前
别再让用户干等了:用 Express + SSE 实现《红楼梦》AI 问答实时输出
javascript·后端·node.js
晓131313 分钟前
【Cocos Creator 3.x】篇——第五章 项目实战优化技术
前端·javascript·游戏引擎
AZaLEan__17 分钟前
JavaScript 基础语法
开发语言·javascript·ecmascript
有梦想的程序星空18 分钟前
【环境配置】使用 Vue CLI 构建 Vue 项目脚手架完整指南
前端·javascript·vue.js
影视飓风TIM21 分钟前
C++ 核心语法笔记:拷贝构造、深浅拷贝与运算符重载
java·开发语言·javascript
之歆22 分钟前
Ajax 进阶:跨域、CORS、JSONP 与请求封装实战
前端·javascript·ajax
sugar__salt29 分钟前
前端Ajax核心原理与实战:从异步机制到接口请求全解析
前端·javascript·ajax
難釋懷30 分钟前
Nginx缓冲区
前端·javascript·nginx
程序猿小泓30 分钟前
2026 前端面试全攻略:手写题、算法与计网/TS 高频考点
前端·javascript·css
老毛肚9 小时前
jeecg-boot-base-core 02 day
javascript·python