vue3访问本地接口地址
时间: 2023-08-04 09:28:04 浏览: 74
在Vue3中访问本地接口地址,可以使用axios来发送HTTP请求。首先需要安装axios库,可以使用npm命令进行安装:
```
npm install axios --save
```
接下来在Vue组件中引入axios库,并使用axios来发送HTTP请求。例如,以下代码演示如何使用axios来获取本地接口地址的数据:
```javascript
<template>
<div>
<ul>
<li v-for="item in items" :key="item.id">{{ item.name }}</li>
</ul>
</div>
</template>
<script>
import axios from 'axios';
export default {
data() {
return {
items: [],
};
},
mounted() {
axios.get('http://localhost:3000/api/items')
.then((response) => {
this.items = response.data;
})
.catch((error) => {
console.error(error);
});
},
};
</script>
```
在上面的代码中,我们使用axios库的get方法来获取本地接口地址`http://localhost:3000/api/items`的数据。在请求成功后,将数据赋值给Vue组件的items属性,最终渲染到页面上。如果请求失败,则在控制台输出错误信息。