如果无法从服务器检索数据,我尝试弹出 sweetalert
我在 main.js 中导入了 Sweet Alert :
import VueSweetalert2 from 'vue-sweetalert2'
import 'sweetalert2/dist/sweetalert2.min.css'
const app = createApp(App)
app.use(VueSweetalert2)
app.mount('#app')
Run Code Online (Sandbox Code Playgroud)
在 Table.vue 组件内,我尝试调用 swal 但收到错误消息 (undefined $this.swal) :
<script>
import axios from 'axios'
import { onMounted, ref } from 'vue'
export default {
setup() {
let transactions = ref([])
onMounted(() => {
getTransactions()
})
async function getTransactions() {
try {
let { data } = await axios.get('http://127.0.0.1:8000/api/transactions')
transactions.value = data.data
} catch(e) {
this.$swal('Something went wrong.')
}
}
return { …Run Code Online (Sandbox Code Playgroud)