一、前端代码
1、定义api
分析这个页面一共有两个远程方法:一个是根基课程id获取课程基本预览信息,第二个是发布课程
getCoursePublishInfoById(id) {
return request({
url: `${api_name}/course-publish-info/${id}`,
method: 'get'
})
},
publishCourse(id) {
return request({
url: `${api_name}/publish-course/${id}`,
method: 'put'
})
}
2、定义数据模型
data() {
return {
saveBtnDisabled: false, // 保存按钮是否禁用
courseId: '', // 所属课程
coursePublish: {}
}
},
3、完善步骤导航
edu/course/chapter.js
previous() {
console.log('previous')
this.$router.push({ path: '/edu/course/info/' + this.courseId })
},
next() {
console.log('next')
this.$router.push({ path: '/edu/course/publish/' + this.courseId })
}
edu/course/pubish.js
<div>
<el-button @click="previous">返回修改</el-button>
<el-button :disabled="saveBtnDisabled" type="primary" @click="publish">发布课程</el-button>
</div>
previous() {
console.log('previous')
this.$router.push({ path: '/edu/course/chapter/' + this.courseId })
},
publish() {
console.log('publish')
course.publishCourse(this.courseId).then(response => {
this.$router.push({ path: '/edu/course/list' })
})
}
4、组件方法定义
import
import course from '@/api/edu/course'
created
created() {
console.log('chapter created')
this.init()
},
获取数据的方法
init() {
if (this.$route.params && this.$route.params.id) {
this.courseId = this.$route.params.id
// 根据id获取课程基本信息
this.fetchCoursePublishInfoById()
}
},
fetchCoursePublishInfoById() {
course.getCoursePublishInfoById(this.courseId).then(response => {
this.coursePublish = response.data.item
})
},
5、组件模板
<template>
<div class="app-container">
<h2 style="text-align: center;">发布新课程</h2>
<el-steps :active="3" process-status="wait" align-center style="margin-bottom: 40px;">
<el-step title="填写课程基本信息"/>
<el-step title="创建课程大纲"/>
<el-step title="最终发布"/>
</el-steps>
<div class="ccInfo">
<img :src="coursePublish.cover">
<div class="main">
<h2>{{ coursePublish.title }}</h2>
<p class="gray"><span>共{{ coursePublish.lessonNum }}课时</span></p>
<p><span>所属分类:{{ coursePublish.subjectLevelOne }} — {{ coursePublish.subjectLevelTwo }}</span></p>
<p>课程讲师:{{ coursePublish.teacherName }}</p>
<h3 class="red">¥{{ coursePublish.price }}</h3>
</div>
</div>
<div>
<el-button @click="previous">返回修改</el-button>
<el-button :disabled="saveBtnDisabled" type="primary" @click="publish">发布课程</el-button>
</div>
</div>
</template>
6、css样式
<style scoped>
.ccInfo {
background: #f5f5f5;
padding: 20px;
overflow: hidden;
border: 1px dashed #DDD;
margin-bottom: 40px;
position: relative;
}
.ccInfo img {
background: #d6d6d6;
width: 500px;
height: 278px;
display: block;
float: left;
border: none;
}
.ccInfo .main {
margin-left: 520px;
}
.ccInfo .main h2 {
font-size: 28px;
margin-bottom: 30px;
line-height: 1;
font-weight: normal;
}
.ccInfo .main p {
margin-bottom: 10px;
word-wrap: break-word;
line-height: 24px;
max-height: 48px;
overflow: hidden;
}
.ccInfo .main p {
margin-bottom: 10px;
word-wrap: break-word;
line-height: 24px;
max-height: 48px;
overflow: hidden;
}
.ccInfo .main h3 {
left: 540px;
bottom: 20px;
line-height: 1;
font-size: 28px;
color: #d32f24;
font-weight: normal;
position: absolute;
}
</style>