Property 'src' does not exist on type 'Element'.
时间: 2024-01-13 07:02:29 浏览: 47
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.
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)