vue-print-nb 组件打印网页

 

一、安装组件

npm install vue-print-nb --save

 

二、引入组件

import Print from "vue-print-nb";
import Vue from "vue";
Vue.use(Print);

三、使用

<div id="printMe" >
    <p>打印内容</p>
</div>
<button v-print="'#printMe'">打印</button>

 四、设置打印样式

在<style>标签里面谢样式

@page {
  margin: 0;
}
@media print {
  .noprint {
    display: none;
  }
}

五、完整代码示例

<template>
  <el-dialog
    :append-to-body="true"
    :close-on-click-modal="false"
    :before-close="cancel"
    :visible.sync="dialog"
    :title="isAdd ? '新增' : '订单详情'"
    width="990px"
  >
    <div id="printTest">
      <el-card>
        <div slot="header">
          <span>收货信息</span>
        </div>
        <div class="text item">用户昵称:{{ form.nickname }}</div>
        <div class="text item">收货人: {{ form.realName }}</div>
        <div class="text item">联系电话: {{ form.userPhone }}</div>
        <div class="text item">收货地址: {{ form.userAddress }}</div>
      </el-card>
      <el-card>
        <div slot="header">
          <span>订单信息</span>
        </div>
        <el-row :gutter="24">
          <el-col :span="12">
            <div class="text item">订单编号: {{ form.orderId }}</div>
            <div class="text item">商品总数: {{ form.totalNum }}</div>
            <div class="text item">支付邮费: {{ form.totalPostage }}</div>
            <div class="text item">实际支付: {{ form.payPrice }}</div>
            <div class="text item">支付方式: {{ form.payTypeName }}</div>
          </el-col>
          <el-col :span="12">
            <div class="text item">订单状态: {{ form.statusName }}</div>
            <div class="text item">商品总价: {{ form.totalPrice }}</div>
            <div class="text item">优惠券金额: {{ form.couponPrice }}</div>
            <div class="text item">创建时间: {{ formatTimeTwo(form.addTime) }}</div>
            <div class="text item">支付时间: {{ formatTimeTwo(form.payTime) }}</div>
          </el-col>
        </el-row>
      </el-card>
      <el-card>
        <div slot="header">
          <span>商品信息</span>
        </div>
        <el-row :gutter="24">
          <!--表格渲染-->
          <el-table
            v-loading="loading"
            :data="form.cartInfoList"
            size="small"
            style="width: 100%;font-size:10px;"
          >
            <el-table-column prop="cartInfoMap.productInfo.barCode" label="商品编码" />

            <el-table-column prop="cartInfoMap.productInfo.storeName" label="商品名称" />
            <el-table-column prop="cartInfoMap.productInfo.attrInfo.suk" label="规格" />
            <el-table-column prop="cartInfoMap.cartNum" label="销售数量" />
            <el-table-column prop="cartInfoMap.productInfo.otPrice" label="市场价格" />
            <el-table-column prop="cartInfoMap.productInfo.price" label="呐喊价格" />
            
            <el-table-column label="库存">
              <template slot-scope="scope">
                <span
                  v-if="scope.row.cartInfoMap.productInfo.stock != '点击查看'"
                >{{scope.row.cartInfoMap.productInfo.stock}}</span>
                <span>
                  <el-button
                    v-if="scope.row.cartInfoMap.productInfo.stock == '点击查看'"
                    size="mini"
                    @click="showStock(scope.row)"
                  >查库存</el-button>
                </span>
              </template>
            </el-table-column>
          </el-table>
        </el-row>
      </el-card>

      <el-card>
        <div slot="header">
          <span>物流信息</span>
        </div>
        <div class="text item">快递公司:{{ form.deliveryName }}</div>
        <div class="text item">快递单号:{{ form.deliveryId }}</div>
      </el-card>
      <el-card>
        <div slot="header">
          <span>备注信息</span>
        </div>
        <div class="text item">{{ form.remark }}</div>
      </el-card>
      <el-card>
        <div slot="header">
          <span>客户备注信息</span>
        </div>
        <div class="text item">{{ form.mark }}</div>
      </el-card>
      
    </div>
    <span slot="footer" class="dialog-footer noprint">
        <el-button @click="dialog = false">关 闭</el-button>
        <el-button type="primary" v-print="'#printTest'">打 印</el-button>
      </span>
  </el-dialog>
</template>

<script>
import { add, edit, levelPriceList } from "@/api/yxStoreOrder";
import { formatTimeTwo } from "@/utils/index";
import { detail } from "@/api/yxStoreProduct";
import Print from "vue-print-nb";
import Vue from "vue";
Vue.use(Print);
export default {
  components: {
    Print
  },
  props: {
    isAdd: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      loading: false,
      dialog: false,
      form: {
        id: "",
        orderId: "",
        uid: "",
        realName: "",
        userPhone: "",
        userAddress: "",
        cartId: "",
        freightPrice: "",
        totalNum: "",
        totalPrice: "",
        totalPostage: "",
        payPrice: "",
        payPostage: "",
        deductionPrice: "",
        couponId: "",
        couponPrice: "",
        paid: "",
        payTime: "",
        payType: "",
        addTime: "",
        status: "",
        refundStatus: "",
        refundReasonWapImg: "",
        refundReasonWapExplain: "",
        refundReasonTime: "",
        refundReasonWap: "",
        refundReason: "",
        refundPrice: "",
        deliveryName: "",
        deliveryType: "",
        deliveryId: "",
        gainIntegral: "",
        useIntegral: "",
        backIntegral: "",
        mark: "",
        isDel: "",
        unique: "",
        remark: "",
        merId: "",
        isMerCheck: "",
        combinationId: "",
        pinkId: "",
        cost: "",
        seckillId: "",
        bargainId: "",
        verifyCode: "",
        storeId: "",
        shippingType: "",
        isChannel: "",
        isRemind: "",
        isSystemDel: ""
      },
      rules: {
        unique: [{ required: true, message: "please enter", trigger: "blur" }]
      }
    };
  },
  methods: {
    formatTimeTwo,
    showStock(row) {
      detail(row.cartInfoMap.productInfo.id).then(res => {
        row.cartInfoMap.productInfo.stock = res.stock;
      });
    },
    cancel() {
      this.resetForm();
    },
    doSubmit() {
      this.loading = true;
      if (this.isAdd) {
        this.doAdd();
      } else this.doEdit();
    },
    doAdd() {
      add(this.form)
        .then(res => {
          this.resetForm();
          this.$notify({
            title: "添加成功",
            type: "success",
            duration: 2500
          });
          this.loading = false;
          this.$parent.init();
        })
        .catch(err => {
          this.loading = false;
          console.log(err.response.data.message);
        });
    },
    doEdit() {
      edit(this.form)
        .then(res => {
          this.resetForm();
          this.$notify({
            title: "修改成功",
            type: "success",
            duration: 2500
          });
          this.loading = false;
          this.$parent.init();
        })
        .catch(err => {
          this.loading = false;
          console.log(err.response.data.message);
        });
    },
    resetForm() {
      this.dialog = false;
      this.form = {
        id: "",
        orderId: "",
        uid: "",
        realName: "",
        userPhone: "",
        userAddress: "",
        cartId: "",
        freightPrice: "",
        totalNum: "",
        totalPrice: "",
        totalPostage: "",
        payPrice: "",
        payPostage: "",
        deductionPrice: "",
        couponId: "",
        couponPrice: "",
        paid: "",
        payTime: "",
        payType: "",
        addTime: "",
        status: "",
        refundStatus: "",
        refundReasonWapImg: "",
        refundReasonWapExplain: "",
        refundReasonTime: "",
        refundReasonWap: "",
        refundReason: "",
        refundPrice: "",
        deliveryName: "",
        deliveryType: "",
        deliveryId: "",
        gainIntegral: "",
        useIntegral: "",
        backIntegral: "",
        mark: "",
        isDel: "",
        unique: "",
        remark: "",
        merId: "",
        isMerCheck: "",
        combinationId: "",
        pinkId: "",
        cost: "",
        seckillId: "",
        bargainId: "",
        verifyCode: "",
        storeId: "",
        shippingType: "",
        isChannel: "",
        isRemind: "",
        isSystemDel: ""
      };
    }
  }
};
</script>

<style scoped>
@page {
  margin: 0;
}
@media print {
  .noprint {
    display: none;
  }
}

.text {
  font-size: 12px;
}

.item {
  padding: 6px 0;
}
</style>

六、遇到的问题

遇到了几个问题 这里列出来

1、无法加载组件

后台报错 : [Vue warn]: Failed to resolve directive: print

解决办法

先引入Vue

再调用 Vue.use(Print);

import Print from "vue-print-nb";
import Vue from "vue";
Vue.use(Print);

2、打印内容不全,显示滚动条

在页面中显示正常,打印预览的时候内容显示不全,显示滚动条,并且不分页

解决办法

使用css控制打印样式

@page {
  margin: 0;
}
@media print {
  .noprint {
    display: none;
  }
}

背景色不能打印

在需要背景色的标签上面加上这个样色即可

.value {
    -webkit-print-color-adjust: exact;
    background-color: #eee;
    padding: 8px;
    font-size: 12px;
    min-width: 80px;
  }

打印多一页空白页

检查要打印的标签是不是用margin属性,margin属性会导致多一页空白页面。有的话想办法用padding来实现相同显示效果。

 

 

 

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于Vue.js中的分页打印,你可以使用vue-print-nb插件来实现。vue-print-nb是一个基于Vue.js的打印插件,可以方便地实现网页的分页打印功能。 首先,你需要在你的Vue项目中安装vue-print-nb插件。可以使用npm或yarn来进行安装: ``` npm install vue-print-nb --save ``` 或者 ``` yarn add vue-print-nb ``` 安装完成后,在你的Vue项目的入口文件(如main.js)中导入vue-print-nb,并将其注册为全局组件: ```javascript import Vue from 'vue' import VuePrintNB from 'vue-print-nb' Vue.use(VuePrintNB) ``` 然后,在你需要进行打印组件中,使用vue-print-nb组件包裹需要打印的内容: ```html <template> <div> <!-- 打印按钮 --> <button @click="print">打印</button> <!-- 需要打印的内容 --> <vue-print-nb ref="printContent"> <div> <!-- ... --> </div> </vue-print-nb> </div> </template> <script> export default { methods: { print() { // 调用vue-print-nb插件的打印方法 this.$refs.printContent.print() } } } </script> ``` 以上代码中,我们在需要打印的内容外部包裹了一个vue-print-nb组件,并给它添加了一个ref属性,用于获取组件的实例。然后,在打印按钮的点击事件中,调用print方法即可触发打印。 请注意,vue-print-nb插件提供了一些配置项,你可以根据需要进行设置。具体的配置信息和更多用法,请参考vue-print-nb的文档。 希望以上信息对你有帮助!如果你还有其他问题,请继续提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值