[英]Importing pipe throwing Typescript property does not exist error
I'm using ionic and have created a custom pipe that takes an object representing image data and returns a URL to that image. 我正在使用ionic,并创建了一个自定义管道,该管道接受一个表示图像数据的对象,并返回该图像的URL。
The pipe file looks like this... 管道文件看起来像这样...
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'eventImageLink',
})
export class EventImageLinkPipe implements PipeTransform {
/**
* Takes a value and makes it lowercase.
*/
transform(value: string, ...args) {
if(value){
return `//res.cloudinary.com/***/image/upload/w_${window.screen.width},d_item_placeholder_e0lign.jpg/${value.image_version}/${args[0]}/${value.original_image_name}`;
}
}
}
I have created a pipes.module.ts
file in the pipes
folder that imports my pipes... 我已经在导入我的管道的
pipes
文件夹中创建了pipes.module.ts
文件...
import { NgModule } from '@angular/core';
import { EventImageLinkPipe } from './event-image-link/event-image-link';
@NgModule({
declarations: [
EventImageLinkPipe
],
imports: [
],
exports: [
EventImageLinkPipe
]
})
export class PipesModule {}
Now before I even import the PipesModule
into a component for use I am getting the following error... 现在,在将
PipesModule
导入组件以供使用之前,我得到以下错误...
Typescript Error 打字稿错误
Property 'image_version' does not exist on type 'string'. 类型'string'上不存在属性'image_version'。
I can't understand why this error is coming up when I haven't even attempted to use it yet and it's quite annoying really. 我什至还没有尝试使用它时,却不明白为什么会出现此错误,这确实很烦人。 Can someone explain why the pipe is being executed and how I can prevent this error from throwing when I haven't passed any data into the pipe yet?
谁能解释为什么执行管道以及在尚未将任何数据传递到管道中时如何防止抛出此错误?
Well, you declared input value: string
which means that you will pass object of type string in it's place in the future. 好吧,您声明了输入
value: string
,这意味着将来将在其位置传递string类型的对象。 Type of string
does not have property of an image_version
as the error says, that is an error and your code simply won't work. 如错误所述,
string
类型不具有image_version
属性,这是一个错误,您的代码根本无法工作。 You're trying to call value.image_version
as part of your return result, you will need to change value
to be of a proper type, not sure which it is in your case. 您尝试调用
value.image_version
作为返回结果的一部分,您将需要将value
更改为正确的类型,不确定是哪种情况。
You could probably have a custom class for it: 您可能为此有一个自定义类:
export class MyImage {
image_version: string;
original_image_name: string;
}
You can then declare a value: MyImage
as type of MyImage
class and Typescript will be fine with it since it contains both properties, image_version
and original_image_name
unlike string
type. 然后,您可以声明一个
value: MyImage
作为MyImage
类和Typescript的类型会很好用,因为它包含与string
类型不同的属性image_version
和original_image_name
。
Also, nothing is being executed really, it's just the TypeScript's static-type checking, which prevents you from doing stupid mistakes like this. 另外,实际上并没有执行任何操作,只是TypeScript的静态类型检查,它可以防止您犯此类愚蠢的错误。 If you don't want to use static-type checking features, you could also just set it as
value
with no type specified. 如果您不想使用静态类型检查功能,也可以将其设置为没有指定类型的
value
。 That way you can pass whatever you want and it will crash at runtime if it's a wrong type of an object instead. 这样,您可以传递所需的任何内容,并且如果对象类型错误,它将在运行时崩溃。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.