feat: dynamic import recipe.json

This commit is contained in:
YunYouJun
2023-07-30 20:59:52 +08:00
parent 38b31a5654
commit b526aae2d0
7 changed files with 26 additions and 23 deletions

View File

@@ -1,5 +1,4 @@
import recipeData from '~/data/recipe.json'
import type { RecipeItem, Recipes } from '~/types'
import type { DbRecipeItem } from 'utils/db'
/**
* 随机几道菜
@@ -7,9 +6,13 @@ import type { RecipeItem, Recipes } from '~/types'
* @returns
*/
export function useRandomRecipe(total: Ref<number>) {
const randomRecipes = ref<RecipeItem[]>([])
function random() {
randomRecipes.value = generateRandomRecipe(recipeData as Recipes, total.value)
const randomRecipes = ref<(DbRecipeItem | undefined)[]>([])
async function random() {
const length = await db.recipes.count()
const randomArr = generateRandomArray(length, total.value)
const result = await db.recipes.bulkGet(randomArr)
if (result)
randomRecipes.value = result.filter(item => !!item)
}
watch(total, () => {