请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

本地存储功能没法正常实现

图片描述选中改变字体后,阅读器里字体和显示所选字体样式的文本会相应改变,刷新页面后也仍有保存,但是有不正常的:
(1)localStorage里fontFamily一直只显示default
(2)选字体时怪异:比如选后B字体后(B字体有生效),再选C字体不生效(仍为B字体);只能选一次default字体后,再选C字体才会生效
【代码是跟着老师敲的,检查了就好几次了 自己找不出原因,希望老师帮忙一下,感谢】


<template>
  <transition name="popup-slide-up">
    <div class="ebook-popup-list" v-show="fontFamilyVisible">
      <div class="ebook-popup-title">
        <div class="ebook-popup-title-icon" @click="hide">
          <span class="icon-down2"></span>
        </div>
        <span class="ebook-popup-title-text">{{$t('book.selectFont')}}</span>
      </div>
      <div class="ebook-popup-list-wrapper">
        <div class="ebook-popup-item" v-for="(item, index) in fontFamilyList"
             :key="index" @click="setFontFamily(item.font)">
    
          <div class="ebook-popup-item-text"  :class="{'selected':isSelected(item)}">
            {{item.font}}
          </div>
          <div class="ebook-popup-item-check"  v-if="isSelected(item)">
            <span class="icon-check"></span>
          </div>
        </div>
      </div>
    </div>
  </transition>
</template>

<script>
  import { FONT_FAMILY } from '../../utils/book'
  import { ebookMixin } from '../../utils/mixin'
  import { saveFontFamily } from '../../utils/localStorage'

  export default {
    mixins: [ebookMixin],
    data() {
      return {
        fontFamilyList: FONT_FAMILY
      }
    },
    methods: {
      setFontFamily(font) {
        this.setDefaultFontFamily(font)
        saveFontFamily(this.fileName, font)
        if (font === 'Default') {
          this.currentBook.rendition.themes.font('Times New Roman')
        } else {
          this.currentBook.rendition.themes.font(font)
        }
      },
      isSelected(item) {
        return this.defaultFontFamily === item.font
      },
      hide() {
        this.setFontFamilyVisible(false)
      }
    }
  }
</script>

<style lang="scss" rel="stylesheet/scss" scoped>
  @import "../../assets/styles/global";
  .ebook-popup-list{
    position: absolute;
    bottom: 0;
    left: 0;
    z-index: 300;
    background: white;
    //赋z-index值大一些:覆盖其他面板
    width: 100%;
    box-shadow: 0 px2rem(-4) px2rem(6) rgba(0, 0, 0, .15);
    .ebook-popup-title{
      position: relative;
      /*使得弹出页面的标题栏中的题目图标可以相对标题栏定位*/
      padding: px2rem(15);
      box-sizing: border-box;
      border-bottom: px2rem(1) solid #b8b9bb;
      // 标题文字居中
      text-align: center;
      @include center;
      .ebook-popup-title-icon{
        position: absolute;
        left: px2rem(15);
        top: 0;
        height: 100%;
        @include center;
        font-size: px2rem(16);
        font-weight: bold;
      }
      .ebook-popup-title-text{
        font-size: px2rem(14);
        font-weight: bold;
      }
    }
    .ebook-popup-list-wrapper{
      .ebook-popup-item{
        display: flex;
        .ebook-popup-item-text{
          flex: 1;
          text-align: left;
          font-size: px2rem(14);
          padding: px2rem(15);
          &.selected{
            color: #346cb9;
            font-weight: bold;
          }
        }
        .ebook-popup-item-check{
          flex: 1;
          text-align: right;
          font-size: px2rem(14);
          font-weight: bold;
          color: #346cb9;
        }
      }
    }
  }
</style>

正在回答

3回答

代码看过了,问题找到了:

1、切换字体没有问题,觉得怪异可能是字体选择的问题,可以尝试换一些字体看看效果;

2、翻页没实现,是因为翻下一页的判断条件写错了,目前源码是这样的:

else if (time < 50 && offsetX < -40) {
  this.nextPage()
}

应该改为:time < 500即可,time < 50说明我们的翻页操作要小于50毫秒

3、你的localStorage部分代码完全没问题,会导致你感觉没生效是因为你的代码中有一个BUG,导致后续代码无法执行,你可以打开chrome浏览器的控制台查看报错原因和代码定位

https://img1.sycdn.imooc.com//szimg/5caddb530001984221640696.jpg

最后发现问题在EbookReader.vue的78行:

if (!defaultTheme) {
  defaultTheme = this.themeList()[0].name
  this.setDefaultTheme(defaultTheme) // 将当前默认主题样式存到vuex
  saveTheme(this.fileName, defaultTheme) // 主题缓存两个地方:1.初始化时 2. 切换主题时
}

这里的: defaultTheme = this.themeList()[0].name写法有误,改为:

  defaultTheme = this.themeList[0].name

即可正常运行了:)

0 回复 有任何疑惑可以回复我~
  • 提问者 慕鱼树 #1
    非常感谢老师!!!~~问题已解决
    回复 有任何疑惑可以回复我~ 2019-04-11 10:46:52
Sam 2019-04-07 21:25:36

可以上传代码看看,应该是切换字体时,保存localstorage没有成功导致

0 回复 有任何疑惑可以回复我~
  • 提问者 慕鱼树 #1
    代码已补发在问题里,麻烦老师帮我看下问题所在
    回复 有任何疑惑可以回复我~ 2019-04-08 00:27:34
  • Sam 回复 提问者 慕鱼树 #2
    代码整体打包上传git后发git地址,这样看不出问题哦,正好连同你提的翻页问题一起看下
    回复 有任何疑惑可以回复我~ 2019-04-08 22:53:48
  • 提问者 慕鱼树 回复 Sam #3
    https://github.com/fishCrush/myEbook.git
    老师项目代母已上传github,麻烦老师帮忙看看问题所在:(1)切换字体时怪异行为(2)翻页功能没有实现
    回复 有任何疑惑可以回复我~ 2019-04-10 18:57:00
提问者 慕鱼树 2019-04-06 12:41:46

https://img1.sycdn.imooc.com//szimg/5ca82d9f000132e408570426.jpg

localStorage.js文件里显示set方法(get,delete同样)未定义,可是已经安装了 web-storage-cache的

0 回复 有任何疑惑可以回复我~
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信