Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

在 script 如何使用 Mock.js #28

Open
bugaio opened this issue Apr 20, 2022 · 2 comments
Open

在 script 如何使用 Mock.js #28

bugaio opened this issue Apr 20, 2022 · 2 comments

Comments

@bugaio
Copy link

bugaio commented Apr 20, 2022

模板有时候 并不是 那么 符合预期值, 所以想 动态 创建值, 类似

// 使用Mock.js
var Random = Mock.Random;
let result = {
     "name": Random.cname(),
    "city": Random.city()
}

out(result);

请问, 我如何实现?

@bugaio
Copy link
Author

bugaio commented Apr 27, 2022

模板有时候 并不是 那么 符合预期值, 所以想 动态 创建值, 类似

// 使用Mock.js
var Random = Mock.Random;
let result = {
     "name": Random.cname(),
    "city": Random.city()
}

out(result);

请问, 我如何实现?

平时 用 Mockjs 多一点, 看了一下 大佬的 代码, fork了一下, 更改 了 部分 代码
可以 安装 w2 install whistle.fake 来代替 vase,
然后 就可以 使用

let obj = mock({
    aa: "@email",
    bb: "@integer(10, 100)",
    cc: random.cname()
})

let obj2 = {
    dd: random.county(true),
    ee: mock("@date")
}

Object.assign(obj, obj2);

out(obj);

其中, mock 为 Mock.mock函数, random被 替换成了 Mock.Random函数

更改的文件有
Snipaste_2022-04-27_10-05-49
如果有 其他 的需求, 可以 自行 再去 添加 修改

@avwo
Copy link
Member

avwo commented Apr 27, 2022

如果不影响之前的用法,也可以提 pr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants