基于 Vue2.X/Vue3.X 项目引入 vue-code-diff/v-code-diff 代码差异插件并使用

基于 Vue2.X/Vue3.X 项目引入 vue-code-diff/v-code-diff 代码差异插件并使用

1、Vue2.X的项目

npm i vue-code-diff@1.2.0

父页面:index.vue

<template>
  <div style="padding: 100px;">
    <CodeDiff />
  </div>
</template>

<script>
import CodeDiff from './components/codeDiff'

export default {
  components: {
    CodeDiff
  },
  data: () => ({

  }),
}
</script>

子组件:codeDiff.vue

<template>
  <div class="vue-code-diff">
    <code-diff
      :old-string="oldValue"
      :new-string="newValue"
      :context="10"
      outputFormat="side-by-side"
    />
  </div>
</template>

<script>
  import CodeDiff from 'vue-code-diff'

  export default {
    components: {
      CodeDiff
    },
    data: () => ({
      // 旧值
      // oldValue: 'Starting  ~',

      oldValue: 'Starting TornaApplication using Java 1.8.0_242 on 帅龍之龍 with PID 520.' +
                '吾,当成赤龍之霸王!沉入红莲炼狱,将真红之光辉指引天道!',

      // 新值
      newValue: 'Starting TornaApplication using Java 1.8.0_242 on 帅龍之龍 with PID 1314.' +
                'Finished Spring Data repository scanning in 36 ms. Found 0 LDAP repository interfaces.' +
                'Started TornaApplication in 4.848 seconds (JVM running for 5.829) ~'

      // newValue: 'Starting  ~'
    }),
    watch: {

    },
    created() {
      
    },
    methods: {
      /**
       * 设置新旧资源值方法句柄
       */
      handleSetResourceValues(oldValue, newValue) {
        this.oldValue = oldValue;
        this.newValue = newValue;
        console.log("setResourceValues =>", this.oldValue, this.newValue);
      }
    }
  };
</script>

<style lang="less" scoped>
  .vue-code-diff {
    margin: 0;
    padding: 0;

    /deep/ .d2h-wrapper {

      .d2h-file-wrapper {
        border: unset;
        border-radius: unset;
        margin-bottom: unset;

        .d2h-files-diff {
          display: flex;

          .d2h-file-side-diff {
            position: relative;
            overflow: hidden;
            background-color: #fee8e9;

            .d2h-code-wrapper {

              .d2h-diff-table {

                .d2h-diff-tbody {

                  tr {

                    // 设置表头的第2个单元格大小高度
                    td:nth-child(2).d2h-info {
                      width: 100%;
                      height: 15px;

                      .d2h-code-side-line {
                        height: 100%;
                      }
                    }

                    // 当单行溢出换行时,需要把行号的高度撑满
                    td.d2h-code-side-linenumber {
                      height: 100%;
                    }

                    // 删除代码的变更
                    td.d2h-del {
                      
                      .d2h-code-side-line {

                        .d2h-code-line-ctn {
                          
                          .hljs {
                            font-family: "Microsoft YaHei","微软雅黑";
                            height: auto;
                            font-size: 14px;
                            white-space: pre-line; // 代码换行
                          }
                        }
                      }
                    }

                    // 新增代码的变更
                    td.d2h-ins {
                      
                      .d2h-code-side-line {

                        .d2h-code-line-ctn {
                          
                          .hljs {
                            font-family: "Microsoft YaHei","微软雅黑";
                            height: auto;
                            font-size: 14px;
                            white-space: pre-line; // 单行溢出换行
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }

          .d2h-file-side-diff + .d2h-file-side-diff {
            background-color: #dfd;
          }
        }
      }
    }
  }
</style>

2、Vue3.X的项目 

npm i v-code-diff@1.3.2

父页面:index.vue

<template>
  <div style="padding: 100px;">
    <CodeDiff />
  </div>
</template>

<script>
import CodeDiff from './components/codeDiff'

export default {
  components: {
    CodeDiff
  },
  data: () => ({

  }),
}
</script>

子组件:codeDiff.vue

<template>
  <div class="v-code-diff">
    <code-diff
      :old-string="oldValue"
      :new-string="newValue"
      :context="10"
      outputFormat="side-by-side"
    />
  </div>
</template>

<script>
  import { CodeDiff } from 'v-code-diff'

  export default {
    components: {
      CodeDiff
    },
    data: () => ({
      // 旧值
      // oldValue: 'Starting  ~',

      oldValue: 'Starting TornaApplication using Java 1.8.0_242 on 帅龍之龍 with PID 520.' +
                '吾,当成赤龍之霸王!沉入红莲炼狱,将真红之光辉指引天道!',

      // 新值
      newValue: 'Starting TornaApplication using Java 1.8.0_242 on 帅龍之龍 with PID 1314.' +
                'Finished Spring Data repository scanning in 36 ms. Found 0 LDAP repository interfaces.' +
                'Started TornaApplication in 4.848 seconds (JVM running for 5.829) ~'

      // newValue: 'Starting  ~'
    }),
    watch: {

    },
    created() {
      
    },
    methods: {
      /**
       * 设置新旧资源值方法句柄
       */
      handleSetResourceValues(oldValue, newValue) {
        this.oldValue = oldValue;
        this.newValue = newValue;
        console.log("setResourceValues =>", this.oldValue, this.newValue);
      }
    }
  };
</script>

<style lang="less" scoped>
  .v-code-diff {
    margin: 0;
    padding: 0;
  }
</style>

3、效果如下

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
vue-code-diff是一个用于在Vue项目中显示代码差异插件。当vue-code-diff不对齐时,可能有以下几个原因和解决方法: 1. 插件版本不兼容:首先要确保vue-code-diff插件的版本与Vue项目的版本兼容。可以通过查看vue-code-diff的文档或者安装最新版本的插件来解决这个问题。 2. 样式冲突:vue-code-diff的展示需要一定的样式支持,可能存在与项目中其他样式库或自定义样式发生冲突的情况。可以通过使用scoped样式、更改样式命名空间或重新设计样式来解决这个问题。 3. 代码格式问题:如果代码Vue组件中使用了不同的缩进或对齐方式,vue-code-diff可能会出现不对齐的情况。在这种情况下,需统一代码的缩进和对齐方式,确保比对的代码结构一致。 4. 数据源问题:vue-code-diff的对比结果是基于提供的数据源进行展示的。如果数据源不正确或者解析出现问题,将导致代码不对齐。检查数据源的正确性,并确保数据用于生成代码差异的函数正确返回对应的数据。 5. 其他问题:如果以上方法都没有解决vue-code-diff不对齐的问题,那可能是插件本身的bug或者其他未知的原因。可以尝试在插件的GitHub页面上提交issue,或者尝试寻找其他的代码对比插件替代解决。 总之,对于vue-code-diff不对齐的问题,需要根据具体情况进行分析和排查,找出具体原因并采取相应的解决方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值