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')); }
不對的地方你們多多指正