14.JSON

1. 引入JSON的三种方法

方法一:axios异步请求

<script>
import axios from "axios";
export default{
    mounted(){
        this.fetchData()
    },
    methods:{
        fetchData(){
            this.$axios.get('static/mockdata/ballStyle.json').then((res)=>{ //static与src同级
                this.ball=res.data.ballStyle
            })
        }
    }
}
</script>

方法二:requie导入

<script>
const ballStyle = require('./items.json')
export default {
  data() {
    return {
      ball: ballStyle
    }
  }
}
</script>

方法三:import导入

<script>
import JiangSu from "../../../static/mockData/JiangSu.json";
export default {
  methods:{
    console.log(JiangSu);
  }
}
</script>

 

2. JSON文件格式

{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "properties": {
                "name": "东城区",
                "center": [116.418757,39.917544]
            }
        },
        {
            "type": "Feature",
            "properties": {
                "name": "朝阳区",
                "center": [116.418757,39.917544]
            }
        }
    ]
}

 

posted @ 2023-07-06 15:18  cjl2019  阅读(5)  评论(0编辑  收藏  举报