我有一个基于vue框架的Web应用程序。我需要将网页URL作为输入发送,并获取该页面的innerHTML,然后通过点击下载文件来将其下载为html文件。
我已编写了vue组件,并添加了codesandbox示例。
<template>
<input type="url" v-model="urlPath" />
<button class="download_button" @click="downloadHtml">Download HTML</button>
</template>
方法
methods: {
downloadHtml() {
let url = this.urlInput;
fetch(url)
.then((res) => res.text())
.then((html) => this.downloadAsFile("report.txt", html));
},
downloadAsFile(name, text) {
const link = this.createDownloadableLink(name, text);
const clickEvent = new MouseEvent('click');
link.dispatchEvent(clickEvent);
},
createDownloadableLink(fileName, content) {
let link = document.createElement("a");
link.download = fileName;
link.href = `data:application/octet-stream,${content}`;
return link;
}
}
以下是Codesandbox的示例 Sandbox链接
基本上,我正在设计Vue组件,其中我提供了URL输入并下载页面内容的innerHTML,并将其保存为HTML。对此部分有什么帮助吗?
method
而不是methods
。 - Estus Flask