vue3.0+以后使用axios请求本地数据有所变化

vue3.0和4.0脚手架中和src同级别的static文件夹变成了public-里面还是放静态资源包括最后打包后index.html:

2.0请求本地文件只需要this.$http.get('../static/you.json')即可获取到数据,但是3.0以后再同样写后端就会报404找不到文件的错误,改成this.$http.get('../public/you.json')也一样

解决方法很简单,只需要去掉../public即可:this.$http.get('/you.json')

比如:

            getAcountList(){
                // let url = '../../public/api/activeList.json'//vue3.0+请求本地json数据,记得把**…/public**去掉。
                let url = '/api/activeList.json'//vue3.0以后,静态文件static文件夹的路径存放改为public文件夹,包括json数据。
                this.$http.get(url).then(res=>{
                    console.log(res)
                })
            }

本地数据成功请求到了

 

posted @ 2020-09-24 15:19  少哨兵  阅读(1682)  评论(0编辑  收藏  举报