我试图在一条路由下显示多个类别,单击第一个类别显示我的产品,但当我单击另一个类别时,它不会显示产品,直到硬刷新我的页面,该页面现在显示类别下的产品列表

<nav class="navbar-end">
          <div
            class="pt-4 p-3"
            v-for="category in categories"
            :key="category._id"
          >
            <router-link :to="`/categories/${category._id}`">
              {{ category.type }}
            </router-link>
          </div>

        </nav>

这是我的脚本标签,用于获取类别下的产品列表


<script>

import axios from "axios";
export default {
  name: "Product",
  components: {},
  data() {
    return {
      category: {},
      categories: [],
      products: [],
      catID: []
    };
  },
  mounted() {
    axios
      .get(
        `http://localhost:5000/api/categories/${this.$route.params.id}`,
        {}
      )
      .then(response => {
        console.log(response);
        this.category = response.data.category;
      })
      .catch(error => {
        console.log(error);
        error;
      });

    axios
      .get(`http://localhost:5000/api/products`, {})
      .then(response => {
        console.log(response);

        this.products = response.data.products;
        const catID = this.category._id;
        // this.products = this.products.filter(
        //  ({ category }) => catID === category._id
        // );
      })
      .catch(error => { 
        error;
      });
   
  },
 
  computed: {
    currentProducts() {
      if (!this.category._id) {
        return this.products;
      }
      return this.products.filter(
        ({ category }) => category._id === this.category._id
      );
    }
  },
  methods: {
    ...mapActions(["addProductToCart"]),
    logout() {
      localStorage.clear();
      this.$router.push("/login");
    }
  }
};
</script>

这是我的分类路由

  {
    path: "/categories/:id",
    name: "Category",
    component: Category
  },

我在单击第一个类别后获得产品,但单击另一个类别会更改我的类别.id url,但直到我硬刷新才更新,它现在显示产品列表.请告诉我如何显示同一类别的产品.具有硬刷新的id路由

推荐答案

这是因为mounted块中的代码只执行一次

watch: {
 '$route.params.id': {
   handler(newVal) {
     this.loadCategories(newVal);
   }
  }
},
mounted() {
 this.loadCategories(this.$route.params.id);
 axios
      .get(`http://localhost:5000/api/products`, {})
      .then(response => {
        console.log(response);

        this.products = response.data.products;
        const catID = this.category._id;
      })
      .catch(error => { 
        error;
      });
},
methods: {
 loadCategories(id) {
  axios
      .get(
        `http://localhost:5000/api/categories/${id}`,
        {}
      )
      .then(response => {
        console.log(response);
        this.category = response.data.category;
      })
      .catch(error => {
        console.log(error);
        error;
      });
  }
}

Vue.js相关问答推荐

将 html 文件移出 dist vite 中的 src 文件夹

如何使用composition api v-model Select 框获取其他JSON 值?

Vue.js 3 没有组件的实例?

如何将外部 svg 转换为 Vue3 组件?

作为props 传递的原始 ref 的 Vue 3 react 性

如何在 Nuxt3 中使用 nuxtjs/auth-next 模块?

我如何使用 vuejs 在我的 url 中编码一个令牌

VueJS中的绑定函数含义

如何在 vuejs 自定义指令中传递动态参数

在 vue 3 中使用 vue-chartjs:createElement 不是函数

为什么 vue 中的 @mouseover 操作不起作用

包含 Vue.js v-if 和 v-for 指令的 HTML 标签在加载时flash

如何在某些异步数据(在 Vuex 存储中)加载之前防止任何路由?

为什么 vue 文档说 refs 不是响应式的,而实际上它们是响应式的

在组件之间共享数据

如何将 Angular 和 Vue 项目合并在一起?

如何确定 Vue 何时完成更新 DOM?

根据路由动态插入 CSS 类到导航栏

克隆元素并附加到 DOM 的正确方法

Vue js在列表中添加动态字段,删除和排序不起作用