CocosCreator之module.exports和exprot使用比较

module.exports—require

单文件单个属性导出

导出声明,TestDefine.js

module.exports = cc.Enum({
    GameMenu: "GameMenu",
    GameMain: "GameMain",
    GameChoose: "GameChoose",
})
var SceneName= cc.Enum({
    GameMenu: "GameMenu",
    GameMain: "GameMain",
    GameChoose: "GameChoose",
})
module.exports=SceneName

导入使用Test.js

var SceneName = require("TestDefine");
cc.log("SceneName.....", SceneName.GameMenu );

单文件多个属性导出

导出声明,TestDefine.js

module.exports.SceneName= cc.Enum({
    GameMenu: "GameMenu",
    GameMain: "GameMain",
    GameChoose: "GameChoose",
})
module.exports.BuildType= cc.Enum({
    Test: 1,
    Release: 2,
})
var SceneName= cc.Enum({
    GameMenu: "GameMenu",
    GameMain: "GameMain",
    GameChoose: "GameChoose",
})
var BuildType= cc.Enum({
    Test: 1,
    Release: 2,
})
module.exports.SceneName=SceneName
module.exports.BuildType=BuildType
var SceneName= cc.Enum({
    GameMenu: "GameMenu",
    GameMain: "GameMain",
    GameChoose: "GameChoose",
})
var BuildType= cc.Enum({
    Test: 1,
    Release: 2,
})
module.exports={SceneName,BuildType}

导入使用Test.js

var {SceneName,BuildType} = require("TestDefine");
cc.log("SceneName.....", SceneName.GameMenu );

Ps:require不需要指定文件路径,只要填入文件名即可,单个导入使用时,承载的变量名可随意;多个导入使用时,{}里的名字需要和导出时的名字一致

 

exprots—import

单文件单个属性导出

导出声明,TestDefine.js

export default  cc.Enum({
    GameMenu: "GameMenu",
    GameMain: "GameMain",
    GameChoose: "GameChoose",
})
var sceneName = cc.Enum({
    GameMenu: "GameMenu",
    GameMain: "GameMain",
    GameChoose: "GameChoose",
})
export default sceneName

导入使用Test.js

import sceneName from "./data/Test/TestDefine";
cc.log("SceneName.....", SceneName.GameMenu );

单文件多个属性导出

导出声明,TestDefine.js

export var sceneName = cc.Enum({
    GameMenu: "GameMenu",
    GameMain: "GameMain",
    GameChoose: "GameChoose",
})
export var BuildType=cc.Enum({
    Test: 1,
    Release: 2,

})
var sceneName = cc.Enum({
    GameMenu: "GameMenu",
    GameMain: "GameMain",
    GameChoose: "GameChoose",
})
 var BuildType=cc.Enum({
    Test: 1,
    Release: 2,
})
export {sceneName,BuildType}

导入使用Test.js

import {sceneName,BuildType} from "./data/Test/TestDefine";
cc.log("SceneName.....", SceneName.GameMenu );

Ps:import需要被导入对象的相对路径,单个导入使用时,承载的变量名可随意;多个导入使用时,{}里的名字需要和导出时的名字一致

你可能感兴趣的:(游戏开发,CocosCreator)