| | |
| | | <template> |
| | | <div> |
| | | <pre v-html="content"></pre> |
| | | <pre v-text="content"></pre> |
| | | </div> |
| | | </template> |
| | | |
| | |
| | | import { ref, watch } from "vue"; |
| | | |
| | | let content = ref(""); |
| | | |
| | | // 获取父组件传递的资源url |
| | | const props = defineProps({ |
| | | previewSrc: { |
| | |
| | | } |
| | | }); |
| | | |
| | | const getContent = (url) => { |
| | | fetch(url) |
| | | .then((res) => res.text()) |
| | | .then((data) => { |
| | | content.value = data; |
| | | }); |
| | | //监控属性previewSrc的变化,如果发生变化,重新获取内容 |
| | | watch( |
| | | () => props.previewSrc, |
| | | () => { |
| | | getContent(); |
| | | } |
| | | ); |
| | | |
| | | const getContent = () => { |
| | | content.value=""; |
| | | fetch(props.previewSrc, { |
| | | responseType: "arraybuffer" |
| | | }).then( |
| | | (response) => { |
| | | return response.arrayBuffer(); |
| | | } |
| | | ).then( |
| | | (data) => { |
| | | const decoder = new TextDecoder('gbk'); |
| | | content.value = decoder.decode(data); |
| | | } |
| | | ); |
| | | }; |
| | | getContent(props.previewSrc); |
| | | getContent(); |
| | | |
| | | |
| | | const comStyle = { |