当前位置:首页 > VUE

vue电影功能实现

2026-03-07 19:17:31VUE

Vue 电影功能实现

数据获取与展示

通过 Vue 的 axiosfetch 从电影 API 获取数据。常见的免费 API 包括 TMDb、OMDb。创建一个 movies 数组存储数据,使用 v-for 在模板中循环渲染电影列表。

<template>
  <div>
    <div v-for="movie in movies" :key="movie.id">
      <h3>{{ movie.title }}</h3>
      <img :src="movie.poster" alt="Poster">
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      movies: []
    };
  },
  async created() {
    const response = await axios.get('API_URL');
    this.movies = response.data.results;
  }
};
</script>

搜索功能

添加搜索输入框,使用 v-model 绑定搜索关键词。通过计算属性或方法过滤电影列表。

vue电影功能实现

<template>
  <input v-model="searchQuery" placeholder="Search movies">
  <div v-for="movie in filteredMovies" :key="movie.id">
    <h3>{{ movie.title }}</h3>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchQuery: '',
      movies: []
    };
  },
  computed: {
    filteredMovies() {
      return this.movies.filter(movie =>
        movie.title.toLowerCase().includes(this.searchQuery.toLowerCase())
      );
    }
  }
};
</script>

分页功能

使用分页组件或手动实现分页逻辑。计算总页数,根据当前页码切片显示数据。

vue电影功能实现

<template>
  <div v-for="movie in paginatedMovies" :key="movie.id">
    <h3>{{ movie.title }}</h3>
  </div>
  <button @click="prevPage">Previous</button>
  <button @click="nextPage">Next</button>
</template>

<script>
export default {
  data() {
    return {
      currentPage: 1,
      itemsPerPage: 10,
      movies: []
    };
  },
  computed: {
    paginatedMovies() {
      const start = (this.currentPage - 1) * this.itemsPerPage;
      const end = start + this.itemsPerPage;
      return this.movies.slice(start, end);
    }
  },
  methods: {
    nextPage() {
      if (this.currentPage * this.itemsPerPage < this.movies.length) {
        this.currentPage++;
      }
    },
    prevPage() {
      if (this.currentPage > 1) {
        this.currentPage--;
      }
    }
  }
};
</script>

电影详情页

使用 Vue Router 实现路由跳转。创建详情页组件,通过路由参数获取电影 ID,并显示详细信息。

// router.js
const routes = [
  { path: '/movie/:id', component: MovieDetail }
];

// MovieDetail.vue
<template>
  <div>
    <h1>{{ movie.title }}</h1>
    <p>{{ movie.overview }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      movie: {}
    };
  },
  async created() {
    const id = this.$route.params.id;
    const response = await axios.get(`API_URL/${id}`);
    this.movie = response.data;
  }
};
</script>

状态管理

对于复杂应用,使用 Vuex 管理电影数据状态。创建 store 模块处理电影数据的获取、搜索和分页。

// store.js
export default new Vuex.Store({
  state: {
    movies: [],
    searchQuery: ''
  },
  mutations: {
    setMovies(state, movies) {
      state.movies = movies;
    },
    setSearchQuery(state, query) {
      state.searchQuery = query;
    }
  },
  actions: {
    async fetchMovies({ commit }) {
      const response = await axios.get('API_URL');
      commit('setMovies', response.data.results);
    }
  },
  getters: {
    filteredMovies: state => {
      return state.movies.filter(movie =>
        movie.title.toLowerCase().includes(state.searchQuery.toLowerCase())
      );
    }
  }
});

通过以上方法,可以实现一个功能完整的电影应用,包括数据展示、搜索、分页和详情查看等功能。

标签: 功能电影
分享给朋友:

相关文章

vue发布动态功能实现

vue发布动态功能实现

实现Vue动态发布功能 前端实现 使用Vue.js构建动态发布表单,包含文本输入和图片上传功能 <template> <div> <textarea v-m…

uniapp实现支付功能

uniapp实现支付功能

支付功能实现概述 在UniApp中实现支付功能通常需要对接第三方支付平台(如微信支付、支付宝支付等)。以下是基于微信支付和支付宝支付的通用实现流程。 微信支付实现步骤 1. 配置支付权限 在微信开放…

uniapp支付功能怎么实现

uniapp支付功能怎么实现

uniapp支付功能实现方法 准备工作 注册微信支付、支付宝等平台的开发者账号,获取必要的商户ID(mch_id)、API密钥(key)、应用ID(appid)等信息。确保项目已配置好相关支付SDK。…

uniapp实现选项卡功能

uniapp实现选项卡功能

实现选项卡功能的基本方法 在uniapp中实现选项卡功能通常需要使用uni-segmented-control组件或自定义样式结合swiper组件。以下是两种常见实现方式: 使用uni-se…

h5实现点击复制功能

h5实现点击复制功能

使用document.execCommand方法(兼容旧浏览器) 通过document.execCommand('copy')实现复制功能,需先选中文本内容。创建一个隐藏的textarea或input…

vue功能实现难点

vue功能实现难点

Vue 功能实现的常见难点 响应式系统深度监听 Vue 的响应式系统基于 Object.defineProperty(Vue 2)或 Proxy(Vue 3),但对嵌套对象或数组的深层属性变化需要额外…