当前位置:首页 > 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实例和数据 在Vue实例中定义电影列表数据,包含电影名称、图片、评分等信息。 new Vue({ el:…

vue实现倍速播放功能

vue实现倍速播放功能

实现倍速播放功能的方法 在Vue中实现倍速播放功能通常涉及HTML5的<video>元素或第三方播放器库(如video.js)的API调用。以下是具体实现方式: 使用原生HTML5 vi…

vue实现账号注册功能

vue实现账号注册功能

实现账号注册功能 在Vue中实现账号注册功能通常需要结合前端表单和后端API交互。以下是一个完整的实现方案: 创建注册表单组件 新建一个Register.vue组件,包含基本的注册表单字段:…

h5实现点击复制功能

h5实现点击复制功能

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

vue实现答题功能

vue实现答题功能

Vue实现答题功能 数据准备 创建一个包含题目、选项和正确答案的数据结构,通常是一个数组对象。每个题目对象包含问题文本、选项数组和正确答案索引。 const questions = [ {…

vue实现功能列表

vue实现功能列表

Vue 实现功能列表的方法 在 Vue 中实现功能列表通常涉及数据绑定、列表渲染、事件处理和状态管理。以下是几种常见的方法: 数据驱动列表渲染 使用 v-for 指令可以轻松实现列表渲染。假设有一个…