项目心得五

本文详细介绍了如何在Vue项目中使用Vant组件Cell结合Vue Router实现动态路由跳转,并展示了如何处理内容加载状态,包括加载中、加载成功、加载失败(包括404错误)的场景。同时,讨论了正文样式的配置,如引入github-markdown-css并防止字号转换,以及图片点击预览功能的实现。
摘要由CSDN通过智能技术生成

vant组件cell,to属性配置路由跳转

<!--
    Cell 单元格的 to 属性和 VueRouter 中的 RouterLink 导航组件的 to 属性用法是一样的
    用法参考链接:https://router.vuejs.org/zh/api/#to
    :to="'/article/' + article.art_id"
    :to="`/article/${article.art_id}`"
    :to="{ name:'路径名称', params:{ 标识符:数据 } }"
-->
<van-cell
    class="article-item"
    :to="{ name: 'article', params: { articleId: article.art_id} }"
  >

动态路径参数的解耦

 {
    path: "/search",
    name: "search",
    component: () => import("@/views/search"),  //懒加载的方式导入路由
  },
  {
    path: "/article/:articleId",
    name: "article",
    component: () => import("@/views/article"),  //懒加载的方式导入路由
    props: true, //动态路径参数的解耦:将动态路径的参数映射到对应组件的props属性中
  },
--------------------------------------------------------------------------------
//对应组件中
  props: {
    // 使用props获取动态路由的数据,接收路由映射过来的动态参数
    // 使用props解耦获得了的动态路由数据,这样我们就可以使用this.articleId 获取动态路由数据 而                
    //不需要使用 this.$route.params.articleId,增加了组件的可复用性 
    articleId: {
      type: [Number, String],
      required: true,
    },
  },

处理内容加载状态:

  • 加载中,显示 loading
  • 加载成功,显示文章详情
  • 加载失败,显示错误提示
    • 如果 404,提示资源不存在
    • 其它的,提示加载失败,用户可以点击重试重新加载
// data定义变量
loading: true, // 加载中的 loading 状态
errStatus: 0 // 失败的状态码
 async loadArticleDetail() {
      this.lodaing = true;
      try {
        // 手动抛错
        // if (Math.random() > 0.6) {
        //   throw new Error("错误");
        // }
        //手动抛错
        const { data } = await getArticleDetail(this.articleId);
        console.log(data);
        data.data.content = data.data.content.replaceAll(
          "https://images.weserv.nl/?url=",
          ""
        );
        this.article = data.data; //vue更新dom是异步的
        // 不使用定时器可以使用nextTick这个api方法
        /*
        this.loading = false
        this.$nextTick(()=>{
            this.previewImage()
        })
        */
        setTimeout(() => {
          // 数据更新之后,调用图片预览
          this.previewImage();
          console.log(this.$refs["article-content"]);
        }, 0);
      } catch (err) {
        if (err.response && err.response.status === 404) {
          this.$toast("访问的文章信息不存在!");
          this.errStatus = 404;
        } else {
          this.$toast("获取文章详情失败!");
        }
      }
      this.lodaing = false;
    },
<!-- 加载中 -->
<div v-if="loading" class="loading-wrap"> ...  </div>
<!-- /加载中 -->

<!-- 加载完成-文章详情 -->
<div v-else-if="article.title" class="article-detail"> ... </div>
<!-- /加载完成-文章详情 -->

<!-- 加载失败:404 -->
<div v-else-if="errStatus === 404" class="error-wrap"> ... </div>
<!-- /加载失败:404 -->

<!-- 加载失败:其它未知错误(例如网络原因或服务端异常) -->
<div v-else class="error-wrap">...</div>
 <!-- /加载失败:其它未知错误(例如网络原因或服务端异常) -->

处理正文样式github-markdown-css

下载样式文件,在style标签中引入:

<style scoped lang="less">
@import "./github-markdown.css"; 

给内容正文标签增加markdown-body样式类:

        <div
          class="article-content markdown-body"
          ref="article-content"
          v-html="article.content"
        >
          文章内容
        </div>

配置不要转换文件中的字号:

// postcss.config.js
module.exports = {
  plugins: {
    // postcss-pxtorem 插件的版本需要 >= 5.0.0
    "postcss-pxtorem": {
      rootValue({ file }) {
        return file.indexOf("vant") !== -1 ? 37.5 : 75;
      },
      propList: ["*"],
      // 配置不要转换的样式资源
      exclude: "github-markdown", 
    },
  },
};

vant组件图片点击预览的使用

    previewImage() {
      // 1、从文章内容中获取到所有的 img DOM 节点
      const articleContent = this.$refs["article-content"];
      const imgs = articleContent.querySelectorAll("img");
      // 2、获取文章内容中所有的图片地址
      const images = [];
      // 3、遍历所有 img 节点,给每个节点注册点击事件
      imgs.forEach((img, index) => {
        images.push(img.src);
        img.onclick = () => {
          ImagePreview({
            images: images,
            startPosition: index,
          });
        };
      });
      // 4、在 img 点击事件处理函数中,调用 ImagePreview 预览
      console.log(images);
    },

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值