vue3+typescript中引入外部文件有几种方法css
(eg:引入echarts)html
第一种方法:vue
1 indext.html中用script引入typescript
<div id="app"></div> <script src="https://cdn.bootcss.com/echarts/4.2.1-rc1/echarts-en.common.min.js"></script>
2 在.vue页面使用,先声明后使用npm
<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')); }
这样就能够正确使用app
第二种方法echarts
1 在项目目录下 npm install @types/echarts --save(能够用@types/下载的这么写,第三种方法是不能够用@types下载的)ide
2 在main.ts中能够全局引入也能够局部引入ui
全局引入代码以下this
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')); }
不对的地方你们多多指正