当前位置:首页 > VUE

vue电影功能实现

2026-02-10 19:25:50VUE

Vue 电影功能实现

电影列表展示

使用 Vue 的 v-for 指令动态渲染电影列表。通过 API 获取电影数据,存储在组件的 data 属性中。

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

<script>
export default {
  data() {
    return {
      movies: []
    }
  },
  created() {
    fetch('https://api.example.com/movies')
      .then(response => response.json())
      .then(data => {
        this.movies = data;
      });
  }
}
</script>

电影搜索功能

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

vue电影功能实现

<template>
  <input v-model="searchQuery" placeholder="Search movies...">
  <div v-for="movie in filteredMovies" :key="movie.id">
    {{ movie.title }}
  </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 Router 实现电影详情页的导航。通过动态路由参数获取电影 ID,并显示详细信息。

vue电影功能实现

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

// MovieDetail.vue
<template>
  <div v-if="movie">
    <h1>{{ movie.title }}</h1>
    <p>{{ movie.plot }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      movie: null
    }
  },
  created() {
    const movieId = this.$route.params.id;
    fetch(`https://api.example.com/movies/${movieId}`)
      .then(response => response.json())
      .then(data => {
        this.movie = data;
      });
  }
}
</script>

电影评分功能

实现星级评分组件,使用自定义事件将评分数据传递给父组件。

<template>
  <div class="star-rating">
    <span 
      v-for="star in 5" 
      :key="star" 
      @click="rate(star)"
      :class="{ 'active': star <= currentRating }"
    >★</span>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentRating: 0
    }
  },
  methods: {
    rate(star) {
      this.currentRating = star;
      this.$emit('rated', star);
    }
  }
}
</script>

电影收藏功能

使用 Vuex 管理全局状态,实现电影收藏功能。通过 mutations 修改收藏状态。

// store.js
const store = new Vuex.Store({
  state: {
    favorites: []
  },
  mutations: {
    toggleFavorite(state, movie) {
      const index = state.favorites.findIndex(fav => fav.id === movie.id);
      if (index === -1) {
        state.favorites.push(movie);
      } else {
        state.favorites.splice(index, 1);
      }
    }
  }
});

// MovieItem.vue
<template>
  <button @click="toggleFavorite(movie)">
    {{ isFavorite ? 'Remove from favorites' : 'Add to favorites' }}
  </button>
</template>

<script>
import { mapState, mapMutations } from 'vuex';

export default {
  props: ['movie'],
  computed: {
    ...mapState(['favorites']),
    isFavorite() {
      return this.favorites.some(fav => fav.id === this.movie.id);
    }
  },
  methods: {
    ...mapMutations(['toggleFavorite'])
  }
}
</script>

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

相关文章

vue实现功能插件

vue实现功能插件

Vue 插件实现方法 Vue 插件通常用于封装全局功能(如指令、过滤器、混入等),以下是实现 Vue 插件的典型方式: 插件基本结构 const MyPlugin = { instal…

vue实现注册功能

vue实现注册功能

实现注册功能的基本步骤 在Vue中实现注册功能通常需要结合前端表单和后端API交互。以下是常见的实现方式: 创建注册表单组件 <template> <div class…

vue实现聊天功能

vue实现聊天功能

Vue实现聊天功能的方法 使用WebSocket实现实时通信 安装WebSocket客户端库如socket.io-client,在Vue组件中建立连接。创建一个消息列表和数据输入框,通过WebSock…

php 实现聊天功能

php 实现聊天功能

PHP 实现聊天功能的方法 使用 WebSocket 和 Ratchet 库 WebSocket 是实现实时聊天的理想选择。Ratchet 是一个 PHP 库,用于处理 WebSocket 连接。…

vue实现筛选功能

vue实现筛选功能

实现筛选功能的基本思路 在Vue中实现筛选功能通常涉及数据绑定、计算属性和方法的使用。通过监听用户输入或选择,动态过滤数据列表。 基础筛选实现 创建一个输入框绑定到Vue实例的data属性,使用计算…

php实现回复功能

php实现回复功能

实现回复功能的基本步骤 在PHP中实现回复功能通常涉及数据库操作、表单处理和前后端交互。以下是实现回复功能的关键步骤: 数据库设计 创建包含回复数据的表结构,例如: CREATE TABLE re…