Vue-3.0 setup函数

本文深入探讨了Vue3中的setup函数,它是组合API的起点,执行于beforeCreate钩子之前,不能直接使用this。setup中声明的数据和函数将供模板使用。在vue3中,推荐使用新的组合API而非vue2的生命周期钩子。文章还列举了vue3的主要生命周期钩子,如onBeforeMount、onMounted等,并指出setup内不支持async/await。

1、setup函数,是组合API的起点。

2、该函数在beforeCreate钩子前执行,因此该函数中不能使用this(会得到undefined)

3、模板中(视图)需要使用的数据和函数,都需要在setup中返回

4、当使用vue3的组合API写法时,不建议用vue2的钩子做一些事情

5、vue3中的生命周期钩子

setup 创建实例前

onBeforeMount  挂载DOM前

onMounted  挂载DOM后

onBeforeUpdate  更新组件前

onUpdated  更新组件后

onBeforeUnmount  卸载销毁前

onUnmounted  卸载销毁后

 6、setup里不能使用async函数修饰符,里面的函数不能使用await

Vue 3文件中引入 `BrowserPrint-3.0.216.min.js` 文件有以下几种方法: ### 全局引入 在项目的入口文件 `index.html` 中使用 `<script>` 标签引入该脚本,这样脚本在整个项目中都可以使用。 ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <link rel="icon" href="/favicon.ico"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Vue 3 App</title> </head> <body> <div id="app"></div> <!-- 假设BrowserPrint-3.0.216.min.js文件在public目录下 --> <script src="BrowserPrint-3.0.216.min.js"></script> <script type="module" src="/src/main.js"></script> </body> </html> ``` ### 在组件中静态导入 如果 `BrowserPrint-3.0.216.min.js` 是一个ES模块,并且导出了一些功能,可以在Vue组件中使用 `import` 语句导入。假设该文件导出了一个名为 `printFunction` 的函数: ```vue <template> <div> <!-- 组件内容 --> </div> </template> <script> import { printFunction } from &#39;./path/to/BrowserPrint-3.0.216.min.js&#39;; export default { setup() { printFunction(); return {}; } } </script> ``` 如果使用 `<script setup>` 语法糖: ```vue <template> <div> <!-- 组件内容 --> </div> </template> <script setup> import { printFunction } from &#39;./path/to/BrowserPrint-3.0.216.min.js&#39;; printFunction(); </script> ``` ### 动态加载脚本 在组件挂载后,手动操作DOM插入script文件。 ```vue <template> <div> <!-- 组件内容 --> </div> </template> <script> export default { mounted() { let script = document.createElement(&#39;script&#39;); script.type = &#39;text/javascript&#39;; script.src = &#39;path/to/BrowserPrint-3.0.216.min.js&#39;; document.body.appendChild(script); } } </script> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值