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 小时前
cJSON使用
json
老前端的功夫4 小时前
Vue 3 性能深度解析:从架构革新到运行时的全面优化
javascript·vue.js·架构
前端 贾公子5 小时前
vue移动端适配方案 === postcss-px-to-viewport
前端·javascript·html
GISer_Jing6 小时前
AI营销增长:4大核心能力+前端落地指南
前端·javascript·人工智能
m0_471199637 小时前
【场景】前端怎么解决离线收银、数据同步异常等场景问题
前端·javascript
栀秋6668 小时前
“无重复字符的最长子串”:从O(n²)哈希优化到滑动窗口封神,再到DP降维打击!
前端·javascript·算法
xhxxx8 小时前
不用 Set,只用两个布尔值:如何用标志位将矩阵置零的空间复杂度压到 O(1)
javascript·算法·面试
有意义8 小时前
斐波那契数列:从递归到优化的完整指南
javascript·算法·面试
Mr.Jessy8 小时前
JavaScript高级:深入对象与内置构造函数
开发语言·前端·javascript·ecmascript