教你使用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 相关文章推荐
JavaScript 高级语法介绍
Jun 15 Javascript
jQuery 获取URL参数的插件
Mar 04 Javascript
ExtJs事件机制基本代码模型和流程解析
Oct 24 Javascript
谷歌浏览器不支持showModalDialog模态对话框的解决方法
Sep 22 Javascript
详细解密jsonp跨域请求
Apr 15 Javascript
jQuery实现两款有动画功能的导航菜单代码
Sep 16 Javascript
Angularjs 制作购物车功能实例代码
Sep 14 Javascript
利用VUE框架,实现列表分页功能示例代码
Jan 12 Javascript
bootstrap table实现x-editable的行单元格编辑及解决数据Empty和支持多样式问题
Aug 10 Javascript
jQuery获取所有父级元素及同级元素及子元素的方法(推荐)
Jan 21 jQuery
javascript+css实现俄罗斯方块小游戏
Jun 28 Javascript
微信小程序实现滚动Tab选项卡
Nov 16 Javascript
详解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
php getsiteurl()函数
2009/09/05 PHP
mysqli_set_charset和SET NAMES使用抉择及优劣分析
2013/01/13 PHP
PHP设计模式(五)适配器模式Adapter实例详解【结构型】
2020/05/02 PHP
Ext 表单布局实例代码
2009/04/30 Javascript
jQuery插件原来如此简单 jQuery插件的机制及实战
2012/02/07 Javascript
调用DOM对象的focus使文本框获得焦点
2014/02/19 Javascript
jquery获取选中的文本和值的方法
2014/07/08 Javascript
js实现显示当前状态的导航效果代码
2015/08/28 Javascript
详解Matlab中 sort 函数用法
2016/03/20 Javascript
原生js实现百叶窗效果及原理介绍
2016/04/12 Javascript
浅析JavaScript中var that=this
2017/02/17 Javascript
js每隔两秒输出数组中的一项(实例)
2017/05/28 Javascript
利用 spin.js 生成等待效果(js 等待效果)
2017/06/25 Javascript
写给vue新手们的vue渲染页面教程
2017/09/01 Javascript
基于Vue.js 2.0实现百度搜索框效果
2020/12/28 Javascript
jQuery UI Draggable + Sortable 结合使用(实例讲解)
2017/09/07 jQuery
基于滚动条位置判断的简单实例
2017/12/14 Javascript
vue bus全局事件中心简单Demo详解
2018/02/26 Javascript
vue 弹出遮罩层样式实例
2020/07/22 Javascript
Nodejs在局域网配置https访问的实现方法
2020/10/17 NodeJs
[01:59]深扒TI7聊天轮盘语音出处 1
2017/05/11 DOTA
[01:00:14]DOTA2官方TI8总决赛纪录片 真视界True Sight
2019/01/16 DOTA
[01:57]2018年度DOTA2最具潜力解说-完美盛典
2018/12/16 DOTA
Python中urllib2模块的8个使用细节分享
2015/01/01 Python
Python快速从注释生成文档的方法
2016/12/26 Python
selenium+python实现自动登录脚本
2018/04/22 Python
pandas DataFrame索引行列的实现
2019/06/04 Python
Python中的类与类型示例详解
2019/07/10 Python
python tkinter实现屏保程序
2019/07/30 Python
基于python3.7利用Motor来异步读写Mongodb提高效率(推荐)
2020/04/29 Python
Europcar澳大利亚官网:全球汽车租赁领域的领导者
2019/03/24 全球购物
Perfume’s Club澳大利亚官网:西班牙领先的在线美容店
2021/02/01 全球购物
Linux的文件类型
2012/03/07 面试题
医药代表个人求职信范本
2013/12/19 职场文书
党员的自我评价范文
2014/01/02 职场文书
2016党员党课心得体会
2016/01/07 职场文书