1.建个文件
2.写个代码 记得export
function createUserId() {
let ghchatId = localStorage.getItem("ghchatId");
if (!ghchatId) {
//没有生成一个
let time1 = Date.parse(new Date()) + ""; //时间戳
let randomNum1 = Math.floor(Math.random() * 10) + ""; //随机数
let userId = time1 + randomNum1;
localStorage.setItem("ghchatId", userId);
}
return ghchatId;
}
export { createUserId };
3.哪用哪引入
<script>
import { createUserId } from "/src/assets/js/common.js";
export default {
created() {
console.log("***********", createUserId());
},
};
</script>