安装快速原型开发工具
npm install -g @vue/cli
npm install -g @vue/cli-service-global
在项目根目录添加配置文件,指定本地服务代理ip和端口号、接口路径如下:
// vue.config.js
module.exports = {
devServer: {
proxy: {
'/user': {
target: 'http://localhost:8081',
pathRewrite: {
'/user': 'user.json'
}
},
'/list': {
target: 'http://localhost:8081',
pathRewrite: {
'/list': 'list.json'
}
},
'/proxy': {
target: 'http://localhost:8081',
pathRewrite: {
'/proxy': 'proxy.json'
}
}
}
}
}
在mock数据目录开启一个静态本地服务开发联调用,http-server 开启一个服务命令后面跟mock所在目录
http-server /src/mock // 在指定目录开启服务
http-server . // 在当前目录开启服务
这里使用 axios 第三方库做数据请求
<script>
import axios from 'axios'
export default {
name: 'list',
data () {
return {
list: [],
priceTxt: ''
}
},
async mounted () {
let { data: { list, price, rate } } = await axios.get('/list')
this.$data.list = list
this.$data.priceTxt = navigator.language === 'zh-CN' ? `¥${price * rate}` : `$${price}`
}
}
</script>