首页
归档
笔记
树洞
搜索
友言

文章详情

Interesting People Record Interesting.

/ JavaScript / 文章详情

Nuxt3 Create a new Vue Composable

Sonder
2024-04-04
1774字
4分钟
浏览 (2.4k)

For Nuxt 3, create a new file inside the composables folder (create it if you don’t have it already) named useBrevoWidget.ts or any name you like:

复制代码
declare global {
   interface Window {
       BrevoConversationsID: string
       BrevoConversations: any
   }
}

export const useBrevoWidget = () => {
   // Load widget on mount
   onMounted(() => {
       const script = document.createElement('script')
       script.async = true
       script.src = 'https://conversations-widget.brevo.com/brevo-conversations.js'
       window.BrevoConversationsID = 'YOUR_CONVERSATIONS_ID'

       script.onload = () => {
           window.BrevoConversations =
               window.BrevoConversations ||
               function () {
                   ;(window.BrevoConversations.q = window.BrevoConversations.q || []).push(arguments)
               }
       }

       if (document.head) {
           document.head.appendChild(script)
       }
   })

   // Destroy widget on unmount
   onBeforeUnmount(() => {
       window?.BrevoConversations('kill', true)
   })

   const openChat = () => {
       window?.BrevoConversations('openChat', true)
   }

   return { openChat }
}

Use your new composable 🎉

复制代码
<template>
   <h1>Contact us</h1>
   <p>Click on the chat bubble</p>
</template>

<script setup lang="ts">
   useBrevoWidget()
</script>
复制代码
<template>
   <h1>Contact us</h1>
   <p>Click on the chat bubble or click this button</p>
   <button @click="openChat">Open chat</button>
</template>

<script setup lang="ts">
   const { openChat } = useBrevoWidget()
</script>
下一篇 / 纯 CSS 实现高度与宽度成比例的效果

🎯 相关文章

💡 推荐文章

🕵️‍♂️ 评论 (0)