【发布时间】:2022-07-21 18:33:25
【问题描述】:
感谢您阅读我的问题。
我正在尝试在运行 Vue.js 3.2 和 axios 的情况下获得新的 <script setup> 语法(组合 API)。
使用正常语法,我的代码看起来像:
<script>
import axios from 'axios'
export default {
name: 'GetRequest',
data () {
return {
infos: null
}
},
mounted () {
axios
.get('https://api.predic8.de/shop/products/')
.then(response => (this.infos = response.data))
}
}
</script>
<template>
<div id="app">
{{ infos }}
</div>
</template>
这很好用,但我为我的项目使用了一个模板 (https://github.com/justboil/admin-one-vue-tailwind),它适用于新的 <script setup>。
我已经找到了一些解决方案,例如:
<script setup>
import {onMounted} from "vue";
const {ref} = require("vue");
const axios = require("axios");
const info = ref([])
onMounted(async () => {
await axios
.get('https://api.predic8.de/shop/products/')
.then(response => {
this.info = response.data
})
})
</script>
<template>
<div id="app">
{{ infos }}
</div>
</template>
但它给了我 'this.infos' 被分配了一个值但从未使用过。
有谁知道我如何将值分配给变量并在<template> 中调用它?
更新:
我通过使用infos.value 而不是this.infos 找到了解决方案
<script setup>
import {onMounted} from "vue"
const {ref} = require("vue")
const axios = require("axios")
const infos = ref([])
onMounted(async () => {
await axios
.get('https://api.predic8.de/shop/products/')
.then(response => {
infos.value = response.data
})
})
</script>
<template>
<div id="app">
{{ infos }}
</div>
</template>
```
【问题讨论】:
标签: javascript vue.js axios