[英]Vue.js new script setup with axios
Thanks for reading my question.感谢您阅读我的问题。
I'm trying to get the new <script setup>
syntax with Vue.js 3.2 and axios running.我正在尝试使用 Vue.js 3.2 和 axios 运行新的
<script setup>
语法。 With the normal syntax my code looks something like:使用正常语法,我的代码看起来像:
<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>
This works just fine, but I use a template ( https://github.com/justboil/admin-one-vue-tailwind ) for my projekt which works with the new <script setup>
.这工作得很好,但我为我的项目使用了一个模板( https://github.com/justboil/admin-one-vue-tailwind ),它适用于新的
<script setup>
。
I already found some solutions like:我已经找到了一些解决方案,例如:
<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>
but it gives me 'this.infos' is assigned a value but never used .但它给了我'this.infos'被分配了一个值但从未使用过。 Does anyone know how I can assigne the value to the variabel and call it in the
<template>
?有谁知道我如何将值分配给变量并在
<template>
中调用它?
I found the solution by using infos.value
instead of this.infos
我通过使用
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>
```
It's better to use inject
for importing axios
in each component.最好使用
inject
在每个组件中导入axios
。 This way you can create some interceptors if they needed as well...这样,您也可以在需要时创建一些拦截器...
First you should install the axios plugin for vue.js
.首先,您应该为
vue.js
安装 axios 插件。
> npm install --save vue-axios
When the installation finished just import the axios
like below example:安装完成后,只需导入
axios
,如下例所示:
import { createApp } from "vue";
import axios from "./plugins/axios";
import VueAxios from "vue-axios";
const app = createApp(App);
// Axios Plugin
app.use(VueAxios, axios);
app.provide("axios", app.config.globalProperties.axios);
import { inject } from "vue";
const axios = inject('axios');
// using axios as usual
Note: I used the <script setup>
inside the component example.注意:我在组件示例中使用了
<script setup>
。
PS: You can create an axios
instance inside the ./plugins/axios.js
file if you need to use interceptors
otherwise just import the axios
as always inside main.js
file! PS:如果需要使用
interceptors
,可以在./plugins/axios.js
文件中创建axios
实例,否则只需在main.js
文件中导入axios
即可!
Any variable declared at the top level script would be accesible from the template.在顶层脚本中声明的任何变量都可以从模板中访问。
More info here https://v3.vuejs.org/api/sfc-script-setup.htm and here https://v3.vuejs.org/guide/composition-api-setup.html更多信息在这里https://v3.vuejs.org/api/sfc-script-setup.htm和这里https://v3.vuejs.org/guide/composition-api-setup.ZFC356FDC70D28C7AEZ86D
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.