vue3+typescript引入外部文件


vue3+typescript中引入外部文件有几种方法

(eg:引入echarts)

第一种方法:

1 indext.html中用script引入

<div id="app"></div>
    <script src="https://cdn.bootcss.com/echarts/4.2.1-rc1/echarts-en.common.min.js"></script>

2 在.vue页面使用,先声明后使用

<script lang="ts">
import { Component , Vue } from 'vue-property-decorator';
declare let echarts:any;
@Component
export default class about extends Vue{
  private mounted(): void{
      this.ech();
  };
  private ech(): void{
    let lineChart =echarts.init(document.getElementById('lineChart'));

}

这样就可以正确使用

第二种方法

1 在项目目录下 npm install @types/echarts --save(可以用@types/下载的这么写,第三种方法是不可以用@types下载的)

2 在main.ts中可以全局引入也可以局部引入

全局引入代码如下

import echarts from 'echarts';
Vue.prototype.$echarts = echarts;

 

局部引入代码如下

let echarts = require('echarts/lib/echarts')

// 引入折线图等组件
require('echarts/lib/chart/line')
require('echarts/lib/chart/bar')
require('echarts/lib/chart/radar')

// 引入提示框和title组件,图例
require('echarts/lib/component/tooltip')
require('echarts/lib/component/title')
require('echarts/lib/component/legend')
require('echarts/lib/component/legendScroll')//图例翻译滚动

Vue.prototype.$echarts = echarts

2 在.vue页面使用

<script lang="ts">
import { Component , Vue } from 'vue-property-decorator';
@Component
export default class about extends Vue{
   public $echarts:any;
  private mounted(): void{
      this.ech();
  };
  private ech(): void{
    let lineChart = this.$echarts.init(document.getElementById('lineChart'));
}

第三种方法

1 1 在项目目录下 npm install vue-awesome-swiper --save

2 在shims-vue.d.ts文件添加代码

declare module 'vue-awesome-swiper' {
  export const Swiper: any
  export const SwiperSlide: any
}

代表从外部注入文件

3 剩下的同第二种方法

第四种方法

1 在项目目录下 npm install @types/echarts --save

2 在.vue页面中直接全局引入也可以按需引入

全局引入代码如下

import echarts from 'echarts';

 

局部引入代码如下

let echarts = require('echarts/lib/echarts')

// 引入折线图等组件
require('echarts/lib/chart/line') require('echarts/lib/chart/bar') require('echarts/lib/chart/radar') // 引入提示框和title组件,图例 require('echarts/lib/component/tooltip') require('echarts/lib/component/title') require('echarts/lib/component/legend') require('echarts/lib/component/legendScroll')//图例翻译滚动 

2 在.vue页面使用

<script lang="ts">
import { Component , Vue } from 'vue-property-decorator';
import echarts from 'echarts';
@Component export default class about extends Vue{ 
private mounted(): void{ this.ech(); };
private ech(): void{ let lineChart = echarts.init(document.getElementById('lineChart')); }

 

不对的地方大家多多指正

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM