Vue.js 中使用 axios 和 window.location.href 实现重定向到 API
"将以下这段代码改成vue""""内容:在Vue中,你可以使用axios来发送GET请求,并使用window.location.href来实现重定向。下面是修改后的代码:\n\njavascript\n<template>\n <div>\n <!-- 在这里添加你的表单或其他元素 -->\n <button @click="redirectToAPI">跳转到API</button>\n </div>\n</template>\n\n<script>\nimport axios from 'axios';\n\nexport default {\n methods: {\n redirectToAPI() {\n const getParam = this.$route.query.s;\n const url = `https://api.vvhan.com/api/ip?s=${getParam}`;\n\n axios.get(url)\n .then(() => {\n window.location.href = url;\n })\n .catch((error) => {\n console.error(error);\n });\n },\n },\n};\n</script>\n\n\n这里使用了Vue Router中的$route对象来获取URL参数。在点击按钮后,会发送GET请求到指定的API,并在请求成功后使用window.location.href进行重定向。请确保你已经安装了axios库,并且在代码中进行了导入。
原文地址: https://www.cveoy.top/t/topic/p8yv 著作权归作者所有。请勿转载和采集!