Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 导入对象并在数组中使用它将使第一项未定义?_Javascript_Reactjs_Typescript - Fatal编程技术网

Javascript 导入对象并在数组中使用它将使第一项未定义?

Javascript 导入对象并在数组中使用它将使第一项未定义?,javascript,reactjs,typescript,Javascript,Reactjs,Typescript,我有几个文件要导入以构建对象: // otis.ts export const otisHeadline = 'Realizing the office of the future for UTC'; export const otisPreview = toCloudinaryUrl('otisPreview1.png'); export const otisClientName = 'Otis'; export const otisClientUrl = 'otis'; export co

我有几个文件要导入以构建对象:

// otis.ts
export const otisHeadline = 'Realizing the office of the future for UTC';
export const otisPreview = toCloudinaryUrl('otisPreview1.png');
export const otisClientName = 'Otis';
export const otisClientUrl = 'otis';

export const otisPreviewItem: PreviewTileProps = {
  headlineText: otisHeadline,
  client: otisClientName,
  clientUrl: otisClientUrl,
  backgroundImage: {
    desktop: otisPreview,
  },
};

然后我将
otisPreviewItem
smPreviewItem
导入主文件:

// ss.ts
import { otisPreviewItem } from './otis';
import { smPreviewItem } from './s-m';

export const module8: Preview = {
  tiles: [otisPreviewItem, smPreviewItem]
}
但是,渲染时会出现错误:

TypeError: Cannot read property 'clientUrl' of undefined
所以在调试过程中,我做了
console.log(module8)
,输出如下:

{ tiles: 
   [ undefined,
     { headlineText: 'Connecting an enterprise brand to a consumer audience',
       client: 'SM',
       clientUrl: 's-m',
       backgroundImage: [Object] } ] }
类型如下:

export interface PreviewTileProps {
  headlineText: string;
  client: string;
  clientUrl: string;
  backgroundImage: {
    desktop: string;
    mobile?: string;
  };
  overlay?: string;
}

export interface Preview {
  tiles: PreviewTileProps[];
}
理论上这应该是可行的。。。但是我不知道为什么
tiles
中的第一项总是未定义。如果我将此对象硬编码到
模块8
,它会工作,但导入它似乎不会

对于我尝试导入并放置在此处的任何对象,都会发生这种情况。不仅仅是
otispreveItem


我在这里遗漏了什么吗?

看起来您导入的文件缺少一个单引号。

看起来您导入的文件缺少一个单引号。

我假设您正在访问组件内的
模块8
。通过访问
此['module8'].clientUrl
来访问render方法。是吗


如果没有,请向我们展示您当前的组件实现好吗?

我假设您通过访问
this['module8'].clientUrl
来访问组件内部的
module8
呈现方法。是吗


如果没有,请向我们展示您当前的组件实现情况好吗?

可能是某个地方的输入错误。那代码可能会在某个地方出错。那代码会起作用的。。。这是因为我编辑了示例。我的编辑器中没有错误。对不起。。。这是因为我编辑了示例。我的编辑器中没有错误。
export interface PreviewTileProps {
  headlineText: string;
  client: string;
  clientUrl: string;
  backgroundImage: {
    desktop: string;
    mobile?: string;
  };
  overlay?: string;
}

export interface Preview {
  tiles: PreviewTileProps[];
}