vue-cli 3.0 读取本地静态json文件-

vue.config.js文件得配置

const express = require('express')
const webpack = require('webpack')
const app = express()
var appData = require('./public/region.json')
var apiRoutes = express.Router();
app.use('/api', apiRoutes)
module.exports = {
	publicPath: './',
	productionSourceMap: false,
	devServer: {
		before(app) {
			app.get('/api/region', (req, res) => {
				res.json({
					data: appData
				})
			})
		},
		proxy: {
			'/test': {
				target: "http://192.168.1.xxx",
				changeOrigin: true,
				pathRewrite: {
					'/test': '/scm'
				}
			},
			
			
		},
	},

}

静态资源得位置如图所示

静态资源位置

 if (process.env.NODE_ENV === 'development') {//需要注意在开发环境和正式环境 请求得url地址的区别
				this.$axios.get('api/region').then((res) => {
					this.options = res.data.data //这个就是json文件里面得json数据
				}, function(err) {
					console.log(err)
				})
			} else {
				this.$axios.get('/region.json').then((res) => {
					this.options = res.data 
				}, function(err) {
					console.log(err)
				})
			}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值