我用Vue3 + laravel创建应用程序,并将其部署到AWS上。虽然我的代码在开发中运行良好,但在生产中不起作用。我认为允诺代码带来了这个问题。
虽然我添加了类别(文本),但没有显示新类别。当我再次添加类别时,类别将全部显示在一起。
删除也不是很好。
有时,添加或删除类别很好。
下面的代码是vue(答应)代码。
// CategoryShow.vue
<script setup>
import axios from "axios";
import { defineProps, onMounted, ref, watch} from "vue"
import { useRoute, useRouter } from 'vue-router'
const route = useRoute()
const router = useRouter()
const props = defineProps({
categoryId: String,
})
const categories = ref([])
const newCategory = ref('')
const category = ref({})
const recipes = ref([])
const newRecipe = ref('')
let categoryId = parseInt(route.params.categoryId)
const submitNewCategory = () => {
return new Promise((resolve) => {
axios.post('/api/categories/store', {
title: newCategory.value
})
resolve();
})
}
const addCategory = async () => {
await submitNewCategory()
const a1 = await getMaxIdCategory()
categoryId =a1.data.id
newCategory.value = ''
router.push({name: 'category.show', params: { categoryId: categoryId }})
}
const deleteCategory = async (id) => {
if (confirm('Are you sure?'))
{
axios.delete('/api/categories/' + id)
if (categories.value.length === 1) {
router.push('/')
return
}
// if deleting the same category that is currently displayed on the screen...
if (categoryId === id)
{
let res = await getMaxIdCategory()
categoryId = res.data.id
router.push({name: 'category.show', params: { categoryId: categoryId }})
return
}
const res1 = await getCategories()
categories.value = res1.data
}
}
const getCategory = () => {
return axios.get('/api/categories/' + categoryId)
}
const getCategories = () => {
return axios.get('/api/categories')
}
const getMaxIdCategory = () => {
return axios.get('/api/max')
}
// ----------------------------
// function relate to recipe
const submitNewRecipe = () => {
axios.post('/api/categories/' + categoryId + '/recipes/store', {
title: newRecipe.value
})
}
const addRecipe = async() => {
submitNewRecipe()
const a2 = await getRecipes()
recipes.value = a2.data
newRecipe.value = ''
}
const getRecipes = () => {
return axios.get('/api/categories/' + categoryId + '/recipes/')
}
const getCategoriesAndCategoryAndRecipes = async () => {
categoryId = parseInt(route.params.categoryId)
const res1 = await getCategories()
const res2 = await getCategory()
const res3 = await getRecipes()
category.value = res2.data
recipes.value = res3.data
categories.value = res1.data
}
getCategoriesAndCategoryAndRecipes()
// -----------------------
onMounted(async () => {
// getCategoriesAndCategoryAndRecipes()
})
watch(route, async () => {
if (!isNaN(categoryId))
{
getCategoriesAndCategoryAndRecipes()
}
})
</script>
<template>
<div class="left-container">
<div class="form-box">
<h4 style="margin-bottom: 20px; margin-top: 10px">RECIPE HOUSE</h4>
<form method="post" v-on:submit.prevent="addCategory">
<input type="text" v-model="newCategory">
</form>
</div>
<ul class="category_ul">
<li v-for="category in categories" :key="category.id">
<button class="btn btn-danger" v-on:click="deleteCategory(category.id)">Delete</button>
<router-link :to="{name: 'category.show', params: {categoryId: category.id }}">
<span>{{ category.title }}</span>
</router-link>
</li>
</ul>
</div>
<div class="right-container">
<span class="icon">
<i class="fas fa-utensils fa-lg"></i>
</span>
<div>
<span>{{ category.title }}</span>
<!-- <span>{{ currentCategory.title }}</span> -->
<form method="post" v-on:submit.prevent="addRecipe">
<input type="text" v-model="newRecipe">
</form>
<ul style="margin-top: 15px;">
<li v-for="recipe in recipes">{{ recipe.title }}</li>
</ul>
</div>
</div>
</template>
getCategories函数使左侧屏幕显示类别。
getCategory函数使右边的屏幕显示单个类别。
getRecipes函数使右侧屏幕显示与类别关联的菜谱
这个网址是我的应用程序的github页面。https://github.com/sakamotosora0116/recipehouse_portfolio/tree/master/resources/js/components
谢谢你的帮助。
发布于 2022-11-22 17:53:53
我认为您在axios请求中缺少了.then。下面是一些例子:
const submitNewCategory = () => {
return new Promise((resolve) => {
axios.post('/api/categories/store', {
title: newCategory.value
}).then(response => {
resolve(response);
})
})
}
使用.then,您正在等待请求的完成。
-编辑--您还可以在.then中添加第二个函数以查找错误。
const submitNewCategory = () => {
return new Promise((resolve, reject) => {
axios.post('/api/categories/store', {
title: newCategory.value
}).then(response => {
resolve(response);
},
error => {
// Can add other treatments for errors
reject(error);
})
})
}
https://stackoverflow.com/questions/74536721
复制相似问题