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()
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐