方案一:推荐

在typescript+Vue的项目中引用echarts,为了加强引用,引入echarts和@types/echarts两个包,一个是工程依赖,一个是声明依赖。

npm install echarts --save
npm install --save @types/echarts

然后在需要引用echarts的组件中引入echarts

<script lang="ts">
……
import echarts from 'echarts';
……
</script>

然后设置好option选项,将图表渲染在DOM里:

// HTML部分
<div ref="chart"></div> // Script部分
option={};
const Chart = echarts.init(this.$refs.chart as HTMLCanvasElement);
Chart.setOption(this.option);

按理来说是这样的,然后我run的时候图表显示也是正确的,但是控制台爆出了类型不兼容的错误,如下:

Argument of type '{ color: string[]; legend: { data: { name: string; textStyle: { color: string; fontSize: number; }; }[]; right: number; top: number; itemWidth: number; itemHeight: number; padding: number; itemGap: number; }; xAxis: { ...; }; yAxis: { ...; }; grid: { ...; }; series: { ...; }[]; }' is not assignable to parameter of type 'EChartOption<SeriesBar | SeriesBoxplot | SeriesCandlestick | SeriesCustom | SeriesEffectScatter | SeriesFunnel | SeriesGauge | SeriesGraph | SeriesHeatmap | SeriesLine | SeriesLines | ... 10 more ... | SeriesTreemap>'.
Types of property 'xAxis' are incompatible.
Type '{ type: string; data: string[]; boundaryGap: boolean; axisLabel: { textStyle: { color: string; }; }; axisLine: { lineStyle: { type: string; color: string[]; width: string; }; }; }' is not assignable to type 'XAxis | XAxis[] | undefined'.
Type '{ type: string; data: string[]; boundaryGap: boolean; axisLabel: { textStyle: { color: string; }; }; axisLine: { lineStyle: { type: string; color: string[]; width: string; }; }; }' is not assignable to type 'XAxis'.
Types of property 'type' are incompatible.
Type 'string' is not assignable to type '"value" | "category" | "time" | "log" | undefined'.
124 | draw() {
125 | const Chart = echarts.init(this.$refs.chart as HTMLCanvasElement);
> 126 | watchChart.setOption(this.option);

最后发现所有在@type/echarts(node_modules/@types/echarts/index.d.ts)里面声明的一些联合变量类型,类似于下图所示的这种,都会出现这种问题:

type Type = 'value' | 'category' | 'time' | 'log'; 是给"'value' | 'category' | 'time' | 'log'"给了一个别名Type  ,详见:  https://ts.xcatliu.com/advanced/type-aliases.html

也可以说是一个字符串字面量类型,详见: https://ts.xcatliu.com/advanced/string-literal-types.html

我在引用处代码里面option配置是:

因为声明的时候type是个联合类型,而不是type?:string这种类型,所以导致直接配置参数出现错误:

最后查看了网上的解决办法:https://zhuanlan.zhihu.com/p/28902584

可以直接 const ec = echarts as any; 但是这种做法很不可取。

最后在 https://jkchao.github.io/typescript-book-chinese/typings/literals.html#%E6%8E%A8%E6%96%AD 找到了解决办法,可以采用一个简单的类型断言来告诉 TypeScript 想推断的字面量:

在本实例中就是:

option = {
xAxis: {
type: ('category' as 'category'),
axisLine: {
lineStyle: {
type: ('dashed' as 'dashed'),
},
},
},
}

到此,完美解决了我的问题(其实是个大神帮我找到的解决方法,在此感谢所有的前辈们)。

方案二:成功但是不够严谨

最后删除了 @types/echarts ,在 echarts.d.ts 中自定义了 echarts 的声明 ,

declare module 'echarts' {
const echarts: any;
export default echarts;
}

然后引用成功并且没有爆出类型错误,但是失去了ts强引用的优势。

方案三:简单直接

直接越过ts的类型检查………………………………………………

Vue+Typescript项目中使用echarts的更多相关文章

  1. vue + typescript 项目起手式

    https://segmentfault.com/a/1190000011744210 2017-10-27 发布 vue + typescript 项目起手式 javascript vue.js t ...

  2. 在 Ionic2 TypeScript 项目中导入第三方 JS 库

    原文发表于我的技术博客 本文分享了在Ionic2 TypeScript 项目中导入第三方 JS 库的方法,供参考. 原文发表于我的技术博客 1. Typings 的方式 因在 TypeScript 中 ...

  3. 在react项目中使用ECharts

    这里我们要在自己搭建的react项目中使用ECharts,我们可以在ECharts官网上看到有一种方式是在 webpack 中使用 ECharts,我们需要的就是这种方法. 我们在使用ECharts之 ...

  4. 在基于ABP框架的前端项目Vue&Element项目中采用电子签章处理文件和打印处理

    在一些内部OA或者流转的文件,或者给一些客户的报价文件.合同,或者一些医院出示的给保险机构的病历资料等,有时候可能都希望快速的使用电子签章的处理方式来给文件盖上特定的印章,本篇随笔介绍基于Vue&am ...

  5. 在基于ABP框架的前端项目Vue&Element项目中采用电子签名的处理

    在前面随笔介绍了<在基于ABP框架的前端项目Vue&Element项目中采用电子签章处理文件和打印处理>的处理,有的时候,我们在流程中或者一些文件签署的时候,需要签上自己的大名,一 ...

  6. 在TypeScript项目中进行BDD测试

    在TypeScript项目中进行BDD测试 什么是BDD? BDD(Behavior-Driven Design)是软件团队的一种工作方式,通过以下方式缩小业务人员和技术人员之间的差距: 鼓励跨角色协 ...

  7. vue项目中引用echarts的几种方式

    准备工作: 首先我们初始化一个vue项目,执行vue init webpack echart,接着我们进入初始化的项目下.安装echarts, npm install echarts -S //或   ...

  8. 在vue项目中封装echarts的正确姿势

    为什么需要封装echarts 每个开发者在制作图表时都需要从头到尾书写一遍完整的option配置,十分冗余 在同一个项目中,各类图表设计十分相似,甚至是相同,没必要一直做重复工作 可能有一些开发者忘记 ...

  9. Vue系列——在vue项目中使用echarts

    该示例使用 vue-cli 脚手架搭建 安装echarts依赖 npm install echarts -S 或者使用国内的淘宝镜像安装 npm install -g cnpm --registry= ...

随机推荐

  1. spring cloud使用Feign做消费端时的eureka.client.registerWithEureka/eureka.client.fetchRegistry是否配置的问题

    记录一下今天工作中的一个小失误. 今天用Feign搭建服务消费者的时候,考虑消费者不需要再提供服务给其他服务,所以不需要注册到注册中心(eureka)中.结果把registerWithEureka和f ...

  2. 开始 第一个自己的python爬虫程序 爬磁力链

    不能一事无成,这么久了学python还是吊着,要落地,落在博客园好了,好像公司也只能上博客园了 昨天看了一篇用正则爬电影天堂的视频,直接拿来用,爬磁力吧,爬好玩的 #导入模块 import reque ...

  3. GDB程序调试

    GDB使用流程 1.编译生成可执行文件: gcc -g tst.c -o tst2.启动GDB gdb tst3. 在main 函数处设置断点 break main4. 运行程序 run GDB 命令 ...

  4. openSUSE安装Qt5

    找了很多资料发现没一个好用又简单的. 终于在wiki.qt.io上找到了一个! 安装方法如下: 1下载安装包,我建议最好下载离线包.下载链接http://download.qt.io/archive/ ...

  5. 在java中浅谈Math类中的常用方法

    通过最近的学习,学到了一些的Math类中的常见方法 package org.stm.demo; public class Test { public static void main(String[] ...

  6. cobbler批量化安装系统

  7. matlab函数每天进步一点点

    1. 读mp4视频 : xyloObj = VideoReader('su35.mp4'); 链接 2. 查看有几个相同的函数和当前使用的函数是哪个路径下的: which -all xxx;    w ...

  8. 虚拟机VMware显示“内部错误”的解决方法

    很有可能是VM服务没有启动,win+R services.msc 进入 “服务”,将VM相关的5个服务全部启动即可.

  9. Lua实现Map

    通过Lua中自带的table来实现一个Map,可以根据键值来插入移除取值 map = {} local this = map function this:new() o = {} setmetatab ...

  10. Python3.7 练习题(三) 将指定目录下的图片进行批量尺寸大小处理

    # 将指定目录下的图片进行批量尺寸大小处理 #修改图片尺寸 导入Image os 快捷键 alt+enter import os from PIL import Image def process_i ...