1. 程式人生 > 其它 >vue中全域性、區域性引入js檔案【vue】、【uniapp】

vue中全域性、區域性引入js檔案【vue】、【uniapp】

技術標籤:uniapp

vue和uniapp中引入js檔案

一、全域性引入

1、main.js當中引入

// 引入個人定義js檔案
import * as tools from './common/util/faser/useage.js'

// 自定義tools
Vue.prototype.$tools=tools;

2、在vue檔案中引用:

{{$tools.getDateTime()}}
this.$tools.getDateTime()

3、工具類檔案必須遵循格式:export function
useage.js中:

// 獲取當天時間 日期。
export function getDateTime()
{ let tmp = new Date(); let str = tmp.getFullYear() + '-' + tmp.getMonth()+1 + '-' + tmp.getDate() + ' ' + tmp.getHours() + ':' + tmp.getMinutes() + ':' + tmp.getSeconds() return str }

4、效果:
效果圖


二、區域性引入

1、vue檔案當中

<script>

import * as tools from './common/util/faser/useage.js'

	export default
{ ……

2、使用

{{tools.getDateTime()}}
this.tools.getDateTime()