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[];
}