【问题标题】:flatmap need additional loaderflatmap 需要额外的加载器
【发布时间】:2021-02-24 02:45:50
【问题描述】:

我在 js 文件中有这个对象数组,并且我已经将 js 文件导入到 vue 文件中。

food.js

food = [
  {
    id: 1,
    name: "burger",
    ingredients: [
       {
         name: "tomato",
         amount: "2 pcs"
       },
       {
         name: "meat",
         amount: "2 pcs"
       }
    ]
  },
  {
    id: 2,
    name: "ice cream",
    ingredients: [
       {
         name: "milk",
         amount: "xxxx"
       },
       {
         name: "food coloring",
         amount: "xxxxx"
       }
    ]
  }
]

脚本

import { food } from '@/data/food'
export default { 
   components: {food},
   data() {
        return {
            ingredientsList: food.flatMap(q => q.ingredients)
        }
    },
}

我想从所有对象中获取所有成分,但是它显示了这个错误

File was processed with these loaders:
 * ./node_modules/cache-loader/dist/cjs.js
 * ./node_modules/vue-loader/lib/index.js
You may need an additional loader to handle the result of these loaders.
|     data() {
|         return {
>             ingredientsList: food.flatMap(q => q.ingredients)
|         }
|     },

我的代码是错误的还是我真的需要额外的加载器?我需要什么样的装载机?我到处搜索,但没有找到任何答案。谢谢

【问题讨论】:

    标签: vue.js vuejs2 flatten


    【解决方案1】:

    food.js

    检查name: burger我想你错过了"的报价,试试name: "bugger"

    export const food = [... 添加到:

    export const food = [
      {
        id: 1,
        name: "burger",
        ingredients: [
           {
             name: "tomato",
             amount: "2 pcs"
           },
           {
             name: "meat",
             amount: "2 pcs"
           }
        ]
      },
      {
        id: 2,
        name: "ice cream",
        ingredients: [
           {
             name: "milk",
             amount: "xxxx"
           },
           {
             name: "food coloring",
             amount: "xxxxx"
           }
        ]
      }
    ]
    

    在vue中

    import { food } from '@/data/food.js'
    export default {
       data() {
            return {
                ingredientsList: food.flatMap(q => q.ingredients)
            }
        }
    }
    

    【讨论】:

    • 啊,是的!我的不好,但它仍然不能解决我的问题
    • 你能告诉我你是如何导入 food.js 的吗?
    • 并删除components: {food},
    • 啊,是的,删除 components: { food}, 对我有用!非常感谢
    • 如果您觉得这有帮助,您可以将我的答案标记为已选中并点赞,谢谢
    【解决方案2】:

    确保food 数组位于data() 方法中以便使用它,或者在它位于另一个文件中时将其导入。

    data() {
      const food = [
        {
          id: 1,
          name: burger,
          ingredients: [
             {
               name: "tomato",
               amount: "2 pcs"
             },
             {
               name: "meat",
               amount: "2 pcs"
             }
          ]
        },
        {
          id: 2,
          name: "ice cream",
          ingredients: [
             {
               name: "milk",
               amount: "xxxx"
             },
             {
               name: "food coloring",
               amount: "xxxxx"
             }
          ]
        }
      ];
    
      return {
        ingredientsList: food.flatMap(q => q.ingredients);
      }
    }
    

    【讨论】:

    • 对不起,我忘了提。食物数组已经从另一个 js 文件中导入了..
    猜你喜欢
    • 2021-07-24
    • 2022-11-11
    • 2020-04-05
    • 1970-01-01
    • 2020-09-12
    • 2020-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多