在Nuxt.js 3中如何创建一个全局组件?

huangapple go评论48阅读模式
英文:

How to create a global component in Nuxt.js 3?

问题

我有一个 Nuxt 3 项目,其中包含多个布局。我想创建一个全局组件,在客户端首次请求我的网站时运行一次。我不希望在用户更改布局时将其卸载。在 Nuxt 3 中是否可能?

以下是全局组件:

<template>
  <component is="script" src="https://js.pusher.com/8.0.0/pusher.min.js" />
</template>

<script setup>
onMounted(() => {
  console.log('mounted here')
  var pusher = new Pusher('APP_ID', {
    cluster: 'CLUSTER_ID',
  })

  var channel = pusher.subscribe('notifications-general')

  channel.bind('PATH', function (data) {
    console.log('here in execute')
    alert(data.notification.body_en)
  })
})
</script>
英文:

I have a nuxt 3 project that has multiple layouts. I'd like to create a global component that runs once when the client first requests my site. And I don't want it to unmount when the user changes layouts. Is this possible with Nuxt 3?

Here is the global component:

&lt;template&gt;
  &lt;component is=&quot;script&quot; src=&quot;https://js.pusher.com/8.0.0/pusher.min.js&quot; /&gt;
&lt;/template&gt;

&lt;script setup&gt;
onMounted(() =&gt; {
  console.log(&#39;mounted here&#39;)
  var pusher = new Pusher(&#39;APP_ID&#39;, {
  cluster: &#39;CLUSTER_ID&#39;,
  })

  var channel = pusher.subscribe(&#39;notifications-general&#39;)

  channel.bind(&#39;PATH, function (data) {
    console.log(&#39;here in execute&#39;)
    alert(data.notification.body_en)
  })
})
&lt;/script&gt;

答案1

得分: 2

Sure, here is the translated content:

尝试将该逻辑放入 src/app.vue 中,理论上这应该在浏览器关闭之前都不会卸载,对吗?
也许这会起作用?

&lt;template&gt;

  &lt;component is=&quot;script&quot; src=&quot;https://js.pusher.com/8.0.0/pusher.min.js&quot; /&gt;

  &lt;nuxt-layout&gt;
    &lt;nuxt-page /&gt;
  &lt;/nuxt-layout&gt;

&lt;/template&gt;

&lt;script setup&gt;
onMounted(() =&gt; {
  console.log(&#39;在此处挂载&#39;)
  var pusher = new Pusher(&#39;APP_ID&#39;, {
  cluster: &#39;CLUSTER_ID&#39;,
  })

  var channel = pusher.subscribe(&#39;notifications-general&#39;)

  channel.bind(&#39;PATH, function (data) {
    console.log(&#39;在此处执行&#39;)
    alert(data.notification.body_en)
  })
})
&lt;/script&gt;
英文:

Try putting that logic in src/app.vue, in theory this should never unmount until the browser it's self is closed?
Perhaps this will work?

&lt;template&gt;

  &lt;component is=&quot;script&quot; src=&quot;https://js.pusher.com/8.0.0/pusher.min.js&quot; /&gt;

  &lt;nuxt-layout&gt;
    &lt;nuxt-page /&gt;
  &lt;/nuxt-layout&gt;

&lt;/template&gt;

&lt;script setup&gt;
onMounted(() =&gt; {
  console.log(&#39;mounted here&#39;)
  var pusher = new Pusher(&#39;APP_ID&#39;, {
  cluster: &#39;CLUSTER_ID&#39;,
  })

  var channel = pusher.subscribe(&#39;notifications-general&#39;)

  channel.bind(&#39;PATH, function (data) {
    console.log(&#39;here in execute&#39;)
    alert(data.notification.body_en)
  })
})
&lt;/script&gt;

huangapple
  • 本文由 发表于 2023年5月25日 21:24:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76332808.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定