教你使用vue-cli快速构建的小说阅读器


Posted in Javascript onMay 13, 2019

项目介绍

主要页面

1、首页home.vue分类展示书籍,幻灯片展示热门推荐

2、搜索search.vue,上拉加载更多

3、书籍详情book.vue加入书架、立即阅读,展示评论,同类书籍推荐

4、书籍内容read.vue,获取目录,存储翻阅的章节位置,

5、书架bookrack.vue,获取加入书架的书单

技术栈

vue、vue-cli、axios、vue-router、vuex、localStorege

  • 入口页面app.vue

分成底部导航 跟 主视图容器 router-view

首页tabbar/Home

  • 包含: components/sub/item 和 components/sub/search 、components/sub/header
  • 结构: banner切换 与 搜索 和 小说分类楼层

小说楼层单独定义了组件 components/sub/item , home循环楼层分类名称,并将楼层分类id传给item组件 :booklistId='{id:item._id}' , item组件用 props: ["booklistId"] 接收分类id, 并根据分类id获取对应的数据

item.vue

mouted:
 this.getlist(this.booklistId.id);

methods:
 getlist(id) {
 //每个分类id下对应的数据 子组件接收父组件传过来的id 获取对应的数据
  bootd(id).then(res => {
  var arrdata = res.data.data;
  arrdata.map(item => {
   this.booklist.push(item.book);
  });
  });
 }

小说详情页components/book/Book.vue

  • 包含: components/sub/yuedu 、mulu、pinglun、
  • 结构: 小说概况与简介,是否加入书架或者继续阅读 ,目录、评论、同类书籍推荐

加入书架/立即阅读(yuedu.vue)组件

book.vue

computed: {
  ...mapState(['calbook','shuajiabook']) //书籍信息 书架数据[]
  },
  methods:{
  addbook(){
   this.flag=!this.flag
   var book= this.calbook; // calbook 是store里存储的书籍信息【 SHEFLBOOK 】
   var carbook = JSON.parse(window.localStorage.getItem('book') || '{}')
   if(!this.flag){ //加入书架
    carbook[book._id] = {
     cover: book.cover,
     flag:!this.flag,
     title: book.title,
     lastChapter:book.lastChapter,
     id: book._id,
     author:book.author,
     chapterIndexCache: 0,
     bookSource: 0,
     pageIndexCache: 0,
    }
    this.setbook(false)
    window.localStorage.setItem('book', JSON.stringify(carbook))
   }else{
    delete carbook[book._id]
    this.setbook(true) //设置的布尔值
    window.localStorage.setItem('book', JSON.stringify(carbook))
   }
  }
 }

立即阅读时进入小说章节 `this.$router.push({name:'read',params:{id:this.booklinks}})`

目录组件 components/sub/mulu.vue

点击目录时,路由跳转进入 read.vue 页面(小说详细章节),并且将默认目录显示出来,书籍id通过路由传给 mulu.vue

book.vue

<router-link :to="{name:'read',params:{id:this.books._id,showMulu:true}}" class="mulu" tag="p">

read.vue包含mulu组件,默认目录隐藏,通过路由传参决定是否显示目录

<mulu :id="id" @readShow='readshows' v-show="showMulu" @closeLayer='backGo()'></mulu>

 // 控制目录显示状态
 created() {
  this.getBook = JSON.parse(window.localStorage.getItem("SHEFLBOOK")); // book/book.vue 存储的书籍信息
  this.getbookhy(this.getBook._id); // 获取小说id所对应的源 
  if(this.$route.params.showMulu){ //从book.vue传过来的参数
   this.showMulu = true
  }
 },

// 子组件mulu.vue发送过来的事件,点击时跳转到对应的章节内容
 readshows(index){
  this.showMulu = false
  this.iss = index
  this.getBookindex()
  this.getcontent(this.booklinkss[this.iss]); //根据booklinkss显示目录章节
  this.$refs.dvtop.scrollTop = 0;
 },

mulu.vue

<li v-for="item in list" :key="item.id" @click='getMulu((item.order)-1)'>{{item.title}} <em style="color:red">{{item.isVip?'vip':null}}</em></li>

  getMulu(i){
   this.$emit('readShow',i) //将点击的章节传给父组件 read.vue
  }

pinglun.vue

评论组件(pinglun.vue),获取路由中的参数书籍id,在item.vue中 <router-link tag='li' :to="{name:'book',params:{id:item._id}}"> 路由跳转到详情,并将书籍id传给书籍详情

created() {
 this.loadMore();
 },

 methods: {
 loadMore() {
  bookpl(this.$route.params.id, this.limit).then(res => { // 获取的item组件的路由参数 书籍id
  if (res.status === 200) {
   this.pinglun = res.data.reviews;
   this.limit += 5;
  }
  });
  this.loading = false;
 }
 }

小说章节详情components/read/read.vue

  • 包含: components/sub/mulu 、
  • 结构:上一章下一章,目录,章节内容

获取localstorege的书籍信息SHEFLBOOK中的(id),[book.vue存储了localstorege], 根据小说id获取 【源--目录--内容】 ,点击目录进入章节详情,将点击的章节存入local,记住每次点击的章节位置,供后续阅读

read.vue

//获取小说资源 每本小说有多个资源 根据小说id获取小说的来源
  getbookhy(id){
  bookhy(id).then(res=>{
   this.bookhylist = res.data;
   this.getmulu(this.bookhylist[0]._id);  // 根据源 获取目录数据 默认第一个源
  })
  },
 getmulu(id){
  this.booklinkss = []; //第N章内容
  this.booktitle = []; //第N章标题 push后数据叠加 现将数组数据清空
  var bookindexs = JSON.parse( window.localStorage.getItem("bookindex") || "{}" ); //章节位置

  bookmulu(id).then(res=>{
  if(res.status==200){
    res.data.chapters.forEach( item => {
   // 内容是根据link来获取的 将link发给服务器,由于// / & # 服务器是无法识别的 所以需要对link转码 最后服务器返回内容 
    this.booklinkss.push(encodeURIComponent(item.link))
    this.booktitle.push(item.title)
   });
  }
  this.iss = bookindexs && bookindexs[this.getBook._id] ? bookindexs[this.getBook._id].bookindex : this.iss;
  this.getcontent(this.booklinkss[this.iss]); // 根据目录 获取内容 
  })
 },

  // 获取内容
  getcontent(link){
   var content = []
   bookcontent(link).then(res => {
    if(res.status == 200){
    var datas = res.data.chapter;
    content.push({
     cpContent:datas.isVip?["vip章节,请购买VIP"]:(datas.cpContent ? datas.cpContent.split("\n") : datas.body.split("\n")),
     title:datas.title
    })
    this.con = content[0]
    }
   })
  },

小说搜索页components/read/search.vue 调用MUI的 mt-loadmore 功能,上拉加载更多,

//获取搜索的书籍
 getList() {
  booksearch(this.keyword).then(res => {
  if (res.data.ok) {
   this.searchList = res.data.books.slice(0, 15); //默认展示前15条数据
  }
  });
 },
  // 上拉加载
 loadBottom() {
  this.allLoaded = true; //上滑时出现加载文字
  booksearch(this.keyword).then(res=>{
   if(this.searchList.length==res.data.books.length){
   this.allLoaded = false
   }else{
   this.searchList = res.data.books.splice(0,this.count*15+15) //每次拉动时在现有基础上加15条 cout++
   this.count++
   this.allLoaded = false 
   }
  })
 },

项目截图

教你使用vue-cli快速构建的小说阅读器教你使用vue-cli快速构建的小说阅读器教你使用vue-cli快速构建的小说阅读器教你使用vue-cli快速构建的小说阅读器教你使用vue-cli快速构建的小说阅读器教你使用vue-cli快速构建的小说阅读器

仓库代码

https://github.com/msisliao/v...

总结

以上所述是小编给大家介绍的使用vue-cli快速构建的小说阅读器,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

Javascript 相关文章推荐
extJs 文本框后面加上说明文字+下拉列表选中值后触发事件
Nov 27 Javascript
jquery下div 的resize事件示例代码
Mar 09 Javascript
基于BootStrap Metronic开发框架经验小结【五】Bootstrap File Input文件上传插件的用法详解
May 12 Javascript
JS实现身份证输入框的输入效果
Aug 21 Javascript
JS从非数组对象转数组的方法小结
Mar 26 Javascript
当vue路由变化时,改变导航栏的样式方法
Aug 22 Javascript
vue项目前端埋点的实现
Mar 06 Javascript
JS获取本地地址及天气的方法实例小结
May 10 Javascript
Vue实现表格批量审核功能实例代码
May 28 Javascript
微信小程序 导入图标实现过程详解
Oct 11 Javascript
vue2.0实现列表数据增加和删除
Jun 17 Javascript
Vue组件简易模拟实现购物车
Dec 21 Vue.js
详解js根据百度地图提供经纬度计算两点距离
May 13 #Javascript
解决前后端分离 vue+springboot 跨域 session+cookie失效问题
May 13 #Javascript
Vue.js@2.6.10更新内置错误处机制Fundebug同步支持相应错误监控
May 13 #Javascript
详解async/await 异步应用的常用场景
May 13 #Javascript
轻松解决JavaScript定时器越走越快的问题
May 13 #Javascript
JavaScript Math对象和调试程序的方法分析
May 13 #Javascript
jsonp跨域获取百度联想词的方法分析
May 13 #Javascript
You might like
Ext面向对象开发实践(续)
2008/11/18 Javascript
jquery 利用show和hidden实现级联菜单示例代码
2013/08/09 Javascript
js子页面获取父页面数据示例
2014/05/15 Javascript
JavaScript基础重点(必看)
2016/07/09 Javascript
原生js实现中奖信息无间隙滚动效果
2017/01/18 Javascript
使用nodejs下载风景壁纸
2017/02/05 NodeJs
Angular多选、全选、批量选择操作实例代码
2017/03/10 Javascript
详解axios在vue中的简单配置与使用
2017/05/10 Javascript
浅谈js中的this问题
2017/08/31 Javascript
js与jQuery实现的用户注册协议倒计时功能实例【三种方法】
2017/11/09 jQuery
详解Javascript 中的 class、构造函数、工厂函数
2017/12/20 Javascript
AjaxUpLoad.js实现文件上传功能
2018/03/02 Javascript
TypeScript的安装、使用、自动编译的实现
2020/04/10 Javascript
jQuery实现图片切换效果
2020/10/19 jQuery
Python实现Tab自动补全和历史命令管理的方法
2015/03/12 Python
Django网络框架之创建虚拟开发环境操作示例
2019/06/06 Python
pandas DataFrame的修改方法(值、列、索引)
2019/08/02 Python
关于ResNeXt网络的pytorch实现
2020/01/14 Python
python将unicode和str互相转化的实现
2020/05/11 Python
python 通过exifread读取照片信息
2020/12/24 Python
比利时家具购买网站:Home24
2019/01/03 全球购物
Keds加拿大官网:购买帆布运动鞋和皮鞋
2019/09/26 全球购物
草莓网中国:StrawberryNet中国
2020/08/17 全球购物
员工培训心得体会
2013/12/30 职场文书
高中同学聚会邀请函
2014/01/11 职场文书
应届大学生简历中的自我评价
2014/01/15 职场文书
新年爱情寄语
2014/04/08 职场文书
优质护理服务演讲稿
2014/05/07 职场文书
自愿解除劳动合同协议书
2014/09/11 职场文书
党员学习中共十八大思想报告
2014/09/12 职场文书
城管执法人员个人对照检查材料思想汇报
2014/09/29 职场文书
班子查摆四风个人对照检查材料思想汇报
2014/10/04 职场文书
2014年质量工作总结
2014/11/22 职场文书
2015年大学生工作总结
2015/04/21 职场文书
2015小学语文教师个人工作总结
2015/05/20 职场文书
JavaScript实现栈结构详细过程
2021/12/06 Javascript