本代码段展示了如何使用 Vue.js 构建一个电影详情页,其中包含电影海报、基本信息、评分、描述、购买按钮、底部导航栏等元素。
首先,需要安装必要的依赖包:
npm install vue vue-router vuex vant echarts echarts-for-vue @wangeditor/editor-for-vue v-calendar vue3-baidu-map-gl
在 main.js
中配置 Vue Router 路由:
import { createRouter, createWebHistory } from 'vue-router';
import MovieDetail from './views/MovieDetail.vue';
const router = createRouter({
history: createWebHistory(),
routes: [
{
path: '/movie/:id',
name: 'MovieDetail',
component: MovieDetail,
},
],
});
export default router;
在 MovieDetail.vue
组件中构建 UI 界面:
<template>
<div class="flex flex-col h-screen bg-gray-900">
<van-nav-bar
title="Movie Description"
left-arrow
fixed
:border="false"
class="bg-gray-900 text-white"
/>
<div class="flex-1 overflow-y-auto">
<div class="bg-gray-800 rounded-t-3xl">
<img
class="w-full h-56 object-cover rounded-t-3xl"
src="https://source.unsplash.com/random/375x211"
alt=""
/>
</div>
<div class="bg-white rounded-b-3xl">
<div class="p-4">
<h1 class="text-2xl font-bold text-gray-800">Mortal Kombat</h1>
<div class="flex items-center mt-2">
<van-tag type="primary" size="small" class="mr-2">Action</van-tag>
<van-tag type="primary" size="small" class="mr-2"
>Adventure</van-tag
>
<van-tag type="primary" size="small" class="mr-2">Fantasy</van-tag>
<van-rate
v-model="score"
disabled
size="16"
class="text-yellow-500"
/>
</div>
<div class="flex items-center mt-2">
<van-icon name="person-fill" class="text-gray-500 mr-2" />
<span class="text-gray-500">16+</span>
<van-icon name="calendar-fill" class="text-gray-500 mx-2" />
<span class="text-gray-500">2021</span>
<van-icon name="time-fill" class="text-gray-500 mr-2" />
<span class="text-gray-500">2h 34min</span>
</div>
<div class="mt-4">
<h2 class="text-xl font-bold text-gray-800">Description</h2>
<div class="text-gray-500 mt-2">
MMA fighter Cole Young seeks out Earth's greatest champions in
order to stand against the enemies of... <a href="#">More</a>
</div>
</div>
<div class="mt-4">
<van-button round block type="danger" class="text-white"
>Buy Tickets</van-button
>
</div>
</div>
</div>
</div>
<van-tabbar active="2" class="bg-gray-900 text-white">
<van-tabbar-item icon="home-o" to="/" />
<van-tabbar-item icon="search" to="/search" />
<van-tabbar-item icon="film" to="/movie" />
<van-tabbar-item icon="person-o" to="/profile" />
</van-tabbar>
</div>
</template>
使用 ECharts 组件绘制电影评分图表:
import { ref } from 'vue';
import * as echarts from 'echarts';
import { h } from "vue";
import { createComponent } from 'echarts-for-vue';
const ECharts = createComponent({echarts, h});
const score = ref(6.2);
使用 WangEditor 富文本编辑器编辑电影描述:
import { Editor, Toolbar } from "@wangeditor/editor-for-vue";
import { onBeforeUnmount, shallowRef } from 'vue';
const editorConfig = ref({
placeholder: '请输入内容...'
});
const editor = shallowRef();
const handleEditorCreated = (editorInstance) => {
// Attach the editor instance to the ref
editor.value = editorInstance;
console.log("editor.value", editor.value, editorInstance)
};
onBeforeUnmount(() => {
// Destroy the editor instance before the component is unmounted
editor.value.destroy();
editor.value = null;
});
使用 VCalendar 日历组件显示电影上映日期:
import { Calendar } from 'v-calendar';
使用 Vue3 Baidu Map 组件展示电影院位置:
import { BMap } from 'vue3-baidu-map-gl';
通过这段代码,我们展示了如何使用 Vue.js 构建一个功能丰富的电影详情页。在开发过程中,我们遇到了以下经验与收获:
未来,该卡片功能可以进一步拓展和优化,例如: