vant手机端网页开发踩坑

1、引入vant包
(1)全局引入

import Vant from ‘vant’
import 'vant/lib/index.css'

Vue.use(Vant)

(2)局部引入

<template>
	<van-list></van-list>
<template>

<script>

import {List } from 'vant'
import 'vant/lib/index.css'

export default{
	components{
		[List.name]:List
	}
}
</script>

踩坑:
引入最新版本报错,原因是vue版本和vant版本不匹配,改为2.12.2版本

npm uninstall vant
npm install vant@2.12.2

2、axios调用接口出现跨域问题
在main.js中添加

axios.defaults.baseURL = '/api'(调用接口会自动添加前缀/api)

在webpack.base.conf.js文件中添加一下代码

devServer: {
    proxy: {
      '/api': {
        // 此处的写法,目的是为了 将 /api 替换成 https://www.baidu.com/
        target: 'https://www.baidu.com',//后台接口地址ip:port
        // 允许跨域
        changeOrigin: true,
        ws: true,
        pathRewrite: {
          '^/api': ''
        }
      }
},

之后调用接口时要去掉http://ip:port,否则还是会出现跨域问题

3、在vue文件中引入less文件报错,地址是对的。

import ‘./style.less’

less-loader版本太高,改为版本5及一下就可以了

npm uninstall less-loader
npm install less-loader@5.0.0

4、ES6将JSON字符串转换为对象

attachment: "[{\"id\":\"121212\",\"name\":\"操作手册.pdf\"}]"

const arr = JSON.parse(attachment)

5、两个数组合并

a.push.apply(a,b)------将b中的所有元素添加到a中,相当于后台的a.addAll(b)
this.dataList.push.apply(this.dataList, _list);

6、使用vue-pdf组件实现预览pdf(不分页,pdf本身带有水印)

npm install vue-pdf@4.2.0
npm install pdfjs-dist@2.5.207(一定要引入这个包,否则后台会报错……catch……)
<template>
  <div class="pdf-box">
    <van-loading type="spinner" v-if="loading"/>
    <pdf v-else v-for="i in numPages" :key="i" :src="src" :page="i"></pdf>
  </div>
</template>

<script>
import pdf from 'vue-pdf'

export default{
	components:{
		pdf
	},

	data() {
    	return {
      		src: '', // pdf路径
      		numPages: 0,
      		loading: true
    	}
  	},
	mounted() {
    	this.initFile()
  	},
	methods:{
		initFile() {
      this.loading = true
      this.$http({
        method: 'get',
        url: this.$route.query.routeUrl,
        responseType: 'blob'
      }).then(res => {
		//得到后台返回文件流
        const blob = new Blob([res.data], {type: 'application/pdf'});
        let pdfSrc = URL.createObjectURL(blob);

        this.src = pdf.createLoadingTask({
          url: pdfSrc,
          cMapUrl: 'https://cdn.jsdelivr.net/npm/pdfjs-dist@2.5.207/cmaps/',//预览文件本身水印
          cMapPacked: true
        })
        this.src.promise.then((pdf) => {
          this.numPages = pdf.numPages
          console.log('this.numPages', this.numPages)
        }).catch(() => {
        })
        this.loading = false

      }).catch(() => {
        this.loading = false
      })


    },
}


}


</script>

<style lang=’less’>
.pdf-box {
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  max-width: 100%;
  margin: 0 auto;
  overflow-x: hidden;
  height: 100%;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  font-size: .28rem;

  span {
    width: 100%;
  }
}
</style>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值