当前位置:首页 > VUE

vue实现影片列表

2026-02-17 08:00:09VUE

Vue 实现影片列表

使用 Vue 实现影片列表可以通过多种方式完成,以下是几种常见的方法:

使用 v-for 渲染列表

通过 v-for 指令动态渲染影片列表数据,结合 :key 确保高效更新。

vue实现影片列表

<template>
  <div>
    <h3>影片列表</h3>
    <ul>
      <li v-for="movie in movies" :key="movie.id">
        {{ movie.title }} - {{ movie.year }}
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      movies: [
        { id: 1, title: "影片1", year: 2020 },
        { id: 2, title: "影片2", year: 2021 },
        { id: 3, title: "影片3", year: 2022 }
      ]
    };
  }
};
</script>

使用组件拆分

将影片项封装为独立组件,提高复用性和可维护性。

<template>
  <div>
    <h3>影片列表</h3>
    <MovieItem v-for="movie in movies" :key="movie.id" :movie="movie" />
  </div>
</template>

<script>
import MovieItem from "./MovieItem.vue";

export default {
  components: { MovieItem },
  data() {
    return {
      movies: [
        { id: 1, title: "影片1", year: 2020 },
        { id: 2, title: "影片2", year: 2021 },
        { id: 3, title: "影片3", year: 2022 }
      ]
    };
  }
};
</script>

从 API 获取数据

通过异步请求获取影片列表数据,通常使用 axiosfetch

vue实现影片列表

<template>
  <div>
    <h3>影片列表</h3>
    <ul v-if="movies.length">
      <li v-for="movie in movies" :key="movie.id">
        {{ movie.title }} - {{ movie.year }}
      </li>
    </ul>
    <p v-else>加载中...</p>
  </div>
</template>

<script>
import axios from "axios";

export default {
  data() {
    return {
      movies: []
    };
  },
  async created() {
    try {
      const response = await axios.get("https://api.example.com/movies");
      this.movies = response.data;
    } catch (error) {
      console.error("获取影片列表失败:", error);
    }
  }
};
</script>

添加搜索和筛选功能

通过计算属性实现影片列表的动态筛选。

<template>
  <div>
    <input v-model="searchQuery" placeholder="搜索影片" />
    <ul>
      <li v-for="movie in filteredMovies" :key="movie.id">
        {{ movie.title }} - {{ movie.year }}
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchQuery: "",
      movies: [
        { id: 1, title: "影片1", year: 2020 },
        { id: 2, title: "影片2", year: 2021 },
        { id: 3, title: "影片3", year: 2022 }
      ]
    };
  },
  computed: {
    filteredMovies() {
      return this.movies.filter(movie =>
        movie.title.toLowerCase().includes(this.searchQuery.toLowerCase())
      );
    }
  }
};
</script>

使用 Vuex 管理状态

在大型应用中,通过 Vuex 集中管理影片列表数据。

<template>
  <div>
    <h3>影片列表</h3>
    <ul>
      <li v-for="movie in movies" :key="movie.id">
        {{ movie.title }} - {{ movie.year }}
      </li>
    </ul>
  </div>
</template>

<script>
import { mapState } from "vuex";

export default {
  computed: {
    ...mapState(["movies"])
  },
  created() {
    this.$store.dispatch("fetchMovies");
  }
};
</script>

以上方法可根据项目需求灵活组合使用。

标签: 影片列表
分享给朋友:

相关文章

vue 实现列表

vue 实现列表

Vue 实现列表的方法 在 Vue 中实现列表通常使用 v-for 指令,结合数组或对象数据渲染动态列表。以下是几种常见的实现方式: 基础列表渲染 使用 v-for 指令遍历数组,渲染列表项。ite…

网页制作css 列表

网页制作css 列表

创建CSS列表样式 使用CSS可以自定义HTML列表(有序列表<ol>和无序列表<ul>)的外观,包括项目符号、缩进、颜色等。以下是一些常见的列表样式方法: 基础列表样式 通…

vue实现列表水印

vue实现列表水印

实现列表水印的方法 在Vue中为列表添加水印可以通过多种方式实现,以下是几种常见的方法: 使用CSS背景图 通过CSS的background-image属性为列表元素添加水印背景。水印可以是文字或图…

vue实现列表页

vue实现列表页

Vue 实现列表页的方法 数据绑定与渲染 使用 v-for 指令动态渲染列表数据,结合 :key 确保渲染性能。示例代码: <template> <ul> <…

vue列表怎么实现

vue列表怎么实现

Vue 列表实现方法 使用 v-for 指令 Vue 中列表渲染主要通过 v-for 指令实现,可以遍历数组或对象。 <template> <ul> <li…

vue实现列表多选

vue实现列表多选

Vue 实现列表多选的方法 使用 v-model 绑定数组 通过 v-model 绑定一个数组,可以轻松实现多选功能。当复选框被选中或取消选中时,数组会自动更新。 <template>…