vue使用iview只打印部分页面元素
时间: 2023-09-18 15:13:06 浏览: 33
您可以使用 `window.print()` 方法将整个页面打印出来,但如果您只想打印部分页面元素,可以通过以下步骤实现:
1. 在需要打印的部分元素上添加一个 ID,例如 `<div id="print-content"></div>`。
2. 使用以下代码获取该元素的 HTML 内容并将其传递给新窗口:
```
let printContents = document.getElementById("print-content").innerHTML;
let originalContents = document.body.innerHTML;
document.body.innerHTML = printContents;
window.print();
document.body.innerHTML = originalContents;
```
3. 在新窗口中打印页面。
请注意,在使用此方法时,您需要在打印前将页面恢复为原始状态,以避免在打印时出现不必要的元素。
相关问题
vue使用iview只打印部分页面内容
您可以使用`iview`的`Print`组件来打印页面的部分内容。以下是实现的步骤:
1. 在需要打印的元素上添加一个特定的类名,例如`print-only`。
2. 使用`Print`组件来进行打印,将需要打印的元素作为`slot`传递给组件。
```vue
<template>
<div>
<div class="print-only">需要打印的内容</div>
<Button type="primary" @click="print">打印</Button>
<Print :printable=".print-only">
<div slot="printable">需要打印的内容</div>
</Print>
</div>
</template>
<script>
import { Button, Print } from 'iview';
export default {
components: {
Button,
Print,
},
methods: {
print() {
this.$refs.print.handlePrint();
},
},
};
</script>
```
3. 然后在`methods`中通过`$refs`获取`Print`组件的实例,并调用`handlePrint()`方法进行打印。
4. 打印完成后,会自动返回原页面。
注意:`Print`组件只能打印通过`slot`传递的内容,因此需要将需要打印的内容作为`slot`传递给组件。
vue使用iview跳转另一页面打印部分页面内容
您可以使用`iview`的`Print`组件来打印页面的部分内容。以下是实现的步骤:
1. 在需要打印的元素上添加一个特定的类名,例如`print-only`。
2. 在需要跳转的页面中使用`router-link`进行跳转,同时将需要打印的元素的类名传递给目标页面。
```vue
<template>
<div>
<div class="print-only">需要打印的内容</div>
<router-link :to="{ name: 'print', params: { printable: 'print-only' } }">跳转并打印</router-link>
</div>
</template>
<script>
export default {
name: 'Page',
};
</script>
```
3. 在目标页面中获取传递过来的类名,并使用`Print`组件进行打印。
```vue
<template>
<div>
<div :class="printable">需要打印的内容</div>
<Button type="primary" @click="print">打印</Button>
<Print :printable=".print-only">
<div slot="printable">需要打印的内容</div>
</Print>
</div>
</template>
<script>
import { Button, Print } from 'iview';
export default {
name: 'PrintPage',
components: {
Button,
Print,
},
computed: {
printable() {
return `.${this.$route.params.printable}`;
},
},
methods: {
print() {
this.$refs.print.handlePrint();
},
},
};
</script>
```
4. 然后在`methods`中通过`$refs`获取`Print`组件的实例,并调用`handlePrint()`方法进行打印。
5. 打印完成后,会自动返回原页面。
注意:`Print`组件只能打印通过`slot`传递的内容,因此需要将需要打印的内容作为`slot`传递给组件。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)