uin-app简明uniapp教程uniapp

uniapp—自定义并引用公用模块

2020-04-03  本文已影响0人  瑟闻风倾

定义一个专用的模块,用来组织和管理这些全局的变量,在需要的页面引入。
优缺点:这种方式维护起来比较方便,但是缺点就是每次都需要引入。
注意:这种方式只支持多个vue页面或多个nvue页面之间公用,vue和nvue之间不公用。如果希望 .vue 和 .nvue 复用一些方法的话,需要采用公用模块的方案,分别在 .vue 和 .nvue 文件中引入。

示例如下

const apiUrl = "http://www.hcoder.net";
const now = Date.now || function () {  
    return new Date().getTime();  
};  
const isArray = Array.isArray || function (obj) {  
    return obj instanceof Array;  
};  
const sayHi = function(){
 console.log('hi');
}

export default {  
    apiUrl,  
    now,  
    isArray,  
    sayHi   
}
<template>
    <view>
        当前时间戳:{{time}}
    </view>
</template>

<script>
import helper from "../../../common/helper.js"
export default {
    data() {
        return {
            time:""
        }
    },
    methods: {
        
    },
    onLoad:function(){
        helper.sayHi();
        console.log("now:" + helper.now());
        this.time = helper.now();
    }
}
</script>

<style>

</style>

上一篇 下一篇

猜你喜欢

热点阅读