uniapp uses ts+vue3+vscode to develop WeChat applet: the name "wx" cannot be found, the download video "filePath" is not of type "DownloadFileOption", etc., save the video to the photo album, etc.

Recently, I developed a small function, which is to save videos to a local photo album. The wx api and uniapp api: downloadFile are used here. However, in vscode, because I used ts development, several errors were reported, such as the type cannot be found, etc. :

Name 'wx' not found.

Parameters of type "{ url: string; filePath: string; success: (res: DownloadSuccessData) => void; fail: (err: any) => void; }" cannot be assigned to type "DownloadFileOption" "parameters.
The object literal can only specify known properties, and 'filePath' is not in type 'DownloadFileOption'.

Problem 1: The name "wx" cannot be found

This requires adding a type declaration in global.d.ts:

declare var wx: any;

Then reopen vscode and you will see the prompt:

 

Question 2: When saving to the album, it prompts "fail invalid video" 

This must be because you did not add the filePath field when using the downloadFile API, so the save failed. But it is normal in the WeChat simulator. Pay attention to this WeChat deception! This path should be written in the following format:

let path = wx.env.USER_DATA_PATH + '/' + '视频名称.mp4'

Question 3: And "filePath" is not in type "DownloadFileOption"

This is because uniapp officials said that this filePath must be added, otherwise it cannot be saved to the album. However, although its official website says so, it does not add this field declaration in the package declaration. Find this declaration file, add this file path in it, then save and reopen vscode, and it will be fine:

Guess you like

Origin blog.csdn.net/weixin_44786530/article/details/134872575