Property 'src' does not exist on type 'Element'.
时间: 2024-01-13 08:02:29 浏览: 317
This error typically occurs when TypeScript is unable to infer the type of an element in the DOM, and assumes it to be of type 'Element', which does not have a 'src' property.
To resolve this error, you can either:
1. Use type assertion to tell TypeScript the type of the element:
```
const img = document.querySelector('img') as HTMLImageElement;
img.src = 'path/to/image.png';
```
2. Use a more specific query selector that targets the element with the 'src' attribute:
```
const img = document.querySelector('img[src]') as HTMLImageElement;
img.src = 'path/to/image.png';
```
By using either of these methods, TypeScript will be able to infer the correct type of the element, and allow you to access its 'src' property without error.
阅读全文