如何通过vue路由器链接从远程api下载base64文件



我需要通过vue-router链接从远程api下载文件。远程api返回base64中的文件。

我添加路线:

const router = new VueRouter({
mode: 'history',
routes: [
{ path: '/files/:id', name: 'file', component: Vue.component('vue-file'), props: true }
],
});

并为其添加组件:

<template>
<div>
</div>
</template>
<script>
export default {
name: 'file',
props: {
id: String
},
mounted: function() {
this.download();
},
methods: {
download: function() {
let $this = this;
axios
.get('apiurl' + encodeURIComponent(this.id))
.then(function (response) {
download(response.data.base64content)
});
}
}
}
</script>

它可以工作,但我不想显示组件模板<template><div></div></template>。我甚至不想刷新屏幕上的内容。有可能吗?

你不应该这样做。Vue组件已完成渲染。如果您不想呈现任何东西,那么作为mixin或插件,您的实现会更好。不过,我认为你可以做一些类似的事情:

render() {
return null;
},

最新更新