有以下几个选项:西红柿、鸡蛋、米饭、葱、面.请使用vue随机选择几种食材,生成的对应食物代码
时间: 2023-07-04 10:16:40 浏览: 206
好的,让我来为您生成一个使用Vue的随机食材配对食谱代码:
```html
<template>
<div>
<p>{{ food }}</p>
</div>
</template>
<script>
export default {
data() {
return {
ingredients: ["西红柿", "鸡蛋", "米饭", "葱", "面"],
meals: [
{ name: "煎蛋盖饭", ingredients: ["鸡蛋", "米饭"] },
{ name: "番茄意大利面", ingredients: ["西红柿", "面"] },
{ name: "葱炒蛋", ingredients: ["鸡蛋", "葱"] },
],
};
},
computed: {
food() {
const randomIngredients = this.ingredients.sort(() => 0.5 - Math.random()).slice(0, 2);
const meal = this.meals[Math.floor(Math.random() * this.meals.length)];
const ingredients = meal.ingredients;
const food = meal.name.replace(/(\${\d})/g, (match) => {
const index = parseInt(match.slice(2, -1));
return randomIngredients[index];
});
return `今天的晚餐是${food},用到的食材有${randomIngredients.join("、")}和${ingredients.filter((item) => !randomIngredients.includes(item)).join("、")}。`;
},
},
};
</script>
```
这个代码将随机选择两种食材,然后随机从三个菜品中选择一个配对食谱,并用所选的食材替换菜品中的占位符。最后,它将返回食谱的描述,其中包括所使用的食材。希望这对你有帮助!
阅读全文