开发者问题收集

创建变量存储时出现错误

2021-06-14
45

我的代码 -

export default Users = [
    {
        id: 1,
        email: "[email protected]",
        username: "user1",
        password: "password",
        userToken: "token123",
    },
    {
        id: 2,
        email: "[email protected]",
        username: "user2",
        password: "pass1234",
        userToken: "token12345",
    },
    {
        id: 3,
        email: "[email protected]",
        username: "testuser",
        password: "testpass",
        userToken: "testtoken",
    },
];

错误 -

ReferenceError: Users is not defined
Module.D:\Code\WhiteHatJr\Apps\unused_seller\model\users.js
D:/Code/WhiteHatJr/Apps/unused_seller/model/users.js:1
> 1 | export default Users = [
  2 |     {
  3 |         id: 1,
  4 |         email: "[email protected]",
View compiled

我正在使用 react native、expo 制作一个应用程序。 但我遇到了这个错误。不知道为什么。 但似乎我用于存储的变量有问题。

2个回答

像这样尝试

const Users = [
    {
        id: 1,
        email: "[email protected]",
        username: "user1",
        password: "password",
        userToken: "token123",
    },
    {
        id: 2,
        email: "[email protected]",
        username: "user2",
        password: "pass1234",
        userToken: "token12345",
    },
    {
        id: 3,
        email: "[email protected]",
        username: "testuser",
        password: "testpass",
        userToken: "testtoken",
    },
];

export default Users
Nikhil bhatia
2021-06-14

请注意,您实际上并不需要该名称 ( Users ),因为导入模块时无论如何都不会使用它。引用文档:

Named exports are useful to export several values. During the import, it is mandatory to use the same name of the corresponding object. [...] But a default export can be imported with any name , for example:

// file test.js
let k; export default k = 12;

// some other file
import m from './test'; 
// note that we have the freedom to use import m instead of import k,
// because k was default export
console.log(m);        // will log 12

由于您不需要该名称,因此您可以使其非常简洁:

export default [
    {
        id: 1,
        email: "[email protected]",
        username: "user1",
        password: "password",
        userToken: "token123",
    },
    // ...
];

但如果您确实需要正确标记这些数据,请考虑完全放弃 default 并改为使用命名导出路径。 在某些团队中 默认导出 考虑有害的

raina77ow
2021-06-14