亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我無法在 vue.js 中對數組實現過濾器

我無法在 vue.js 中對數組實現過濾器

茅侃侃 2023-11-12 21:52:37
我已經找了很長一段時間了,但作為一個新手,我找不到答案。我想用我認為語法錯誤的屬性 id 來過濾我的數組。感謝您的幫助成分export default {  props: ["user", "recette"],  data() {    return { email: this.$route.params.email };  },  components: {},  methods: {},  computed: {    filteredItems: function () {      return this.recette.filter((recettes) => {        return recettes.cat_recetteId === 1;      });    },  },};看法<template>  <div>    <myrecette :recette="recette"/>    <myfooter />  </div></template><script>import myrecette from "../components/recette";import myfooter from "../components/myfooter";export default {  name: "",  data() {    return {      recette: "",      user: "",    };  },  components: {    myrecette,    myfooter,  },  created: function() {    this.axios.get("http://localhost:3000/recette/all_recette/").then((res) => {      (this.recette = res.data.recette),        this.axios          .get(            "http://localhost:3000/user/rec_user/" + this.$route.params.email          )          .then((res) => {            this.user = res.data.user;          });    });  },};</script><style scoped></style>節點router.get("/all_recette", (req, res) => {    db.recette        .findAll({            include: { all: true },        })        .then((recette) => {            if (recette) {                res.status(200).json({                    recette: recette,                });            } else {                res.json("il n'y a pas de recettes");            }        })        .catch(err => {            res.json(err);        });});這是我的完整代碼以及我的節點路線。我的錯誤返回是vue.runtime.esm.js?2b0e:619 [Vue warn]: 渲染錯誤:“TypeError: this.recette.filter 不是函數”
查看完整描述

2 回答

?
慕標5832272

TA貢獻1966條經驗 獲得超4個贊

該過濾器的工作原理是保留返回 的項目true,因此如果您希望所有項目的 a 均為cat_recetteId1,則可以將其更改為:


computed: {

  filteredItems: function() {

    if (!this.recette) return [];

    return this.recette.filter((recettes) => {

      return recettes.cat_recetteId === 1;

    });

  },

},

在大多數情況下,在計算內部使用箭頭函數也是一種很好的做法。


查看完整回答
反對 回復 2023-11-12
?
動漫人物

TA貢獻1815條經驗 獲得超10個贊

您的過濾器回調函數應返回true或false。您 1) 不返回任何內容,2) 分配一個值 (=),而不是進行比較 (==/===)。


computed: {

    filteredItems: function() {

      return this.recette.filter(function(recettes) {

        return recettes.cat_recetteId === 1;

      });

    },

  },


查看完整回答
反對 回復 2023-11-12
  • 2 回答
  • 0 關注
  • 199 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號