当前位置:首页 > 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中实现按钮点击功能可以通过v-on指令或@缩写来绑定事件。以下是几种常见的实现方式: 使用v-on指令 通过v-on:click绑定点击事件,触发定义在methods中的方…

vue实现退出功能

vue实现退出功能

Vue 实现退出功能的方法 在 Vue 中实现退出功能通常涉及清除用户登录状态、跳转登录页等操作。以下是几种常见的实现方式: 清除登录状态并跳转 使用 Vue Router 进行页面跳转,同时清除本…

vue实现修改功能

vue实现修改功能

Vue 实现修改功能的方法 数据绑定与表单处理 使用v-model实现双向数据绑定,快速获取表单输入值。适用于表单类修改场景: <template> <input v-mode…

vue实现订单功能

vue实现订单功能

实现订单功能的核心模块 订单功能通常包含商品展示、购物车管理、订单生成、支付流程等模块。Vue的响应式特性和组件化开发非常适合此类需求。 商品展示组件 使用v-for渲染商品列表,配合计算属性实现筛…

vue实现返回功能

vue实现返回功能

Vue 实现返回功能的方法 在 Vue 中实现返回功能通常涉及以下几种方式,具体取决于应用场景和需求。 使用 window.history API 通过调用浏览器原生的 history API 实现…

vue实现目录功能

vue实现目录功能

Vue实现目录功能的方法 基于路由的目录结构 在Vue项目中,可以通过路由配置自动生成目录结构。使用vue-router的routes数组可以定义页面层级关系,结合递归组件渲染目录树。 // rou…