美文网首页
VUE项目全局引入外部js的方法

VUE项目全局引入外部js的方法

作者: zkzhengmeng | 来源:发表于2019-07-16 16:24 被阅读0次

1 . 新建getTome.js文件

function GetDateTime() {
    var date = new Date();
    var seperator1 = "-";
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var strDate = date.getDate(); //
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = year + seperator1 + month + seperator1 + strDate;
    return currentdate;
}
    export default {
       GetDateTime 
    }

2.在main.js中引入

import Vue from 'vue'
import App from './App.vue'
import router from './router'
import axios from "axios"
import GetDateTime from './getData.js'  //(路径根据js所在路径填写)
Vue.prototype.$time = GetDateTime;  //其中$time为新命的名。

3.在home.vue中引用

<template>
    <div class="hello">
        <h3 @click="getData">这个是home页面</h3>
    </div>
</template>
<script>
    export default {
        name: "hello",
        data(){
            return{
                num:'12'
            }
        },
        methods: {
            getData() {
                            console.log(this.$time.GetDateTime()) //调用外部js方法
            }
        }
    }
</script>

相关文章

网友评论

      本文标题:VUE项目全局引入外部js的方法

      本文链接:https://www.haomeiwen.com/subject/ojxclctx.html