简体   繁体   English

NuxtJS/VueJS:如何知道页面是否仅在客户端呈现?

[英]NuxtJS/VueJS: How to know if page was rendered on client-side only?

Currently I create a html5 video player component for an universal nuxtjs/vuejs app.目前,我为通用 nuxtjs/vuejs 应用程序创建了 html5 视频播放器组件。 There is an autoplay attribute for the video tag to start videos after navigate to them.视频标签有一个自动播放属性,可以在导航到视频后开始播放视频。 Usually browsers don't do that directly after page load, it's prohibited.通常浏览器不会在页面加载后直接这样做,这是被禁止的。 I need a variable in my component to know if autoplay will be possible to style elements based on this information.我需要在我的组件中使用一个变量来了解自动播放是否可以根据此信息设置元素样式。 In other words: The variable should be true if the current page was only rendered on client-side, but false if it was rendered on server-side first.换句话说:如果当前页面仅在客户端呈现,则该变量应该为真,但如果它首先在服务器端呈现,则该变量应该为假。

It's not possible to work with "window.history.length" because the autoplay will also not be possible after refresh, although this would not have an effect on the history length.无法使用“window.history.length”,因为刷新后也无法自动播放,尽管这不会影响历史长度。

Also it's not possible to set a variable in the "created" method since it will be called on server- and client-side too.此外,不可能在“created”方法中设置变量,因为它也会在服务器端和客户端调用。

In nuxt - there is the <client-only> component that renders components only on the client side在 nuxt - 有<client-only>组件仅在客户端呈现组件

<template>
  <div>
    <sidebar />
    <client-only placeholder="Loading...">
      <!-- this component will only be rendered on client-side -->
      <comments />
    </client-only>
  </div>
</template>

In nuxt, there are is asyncData which is run beforeCreate() , ie you can retrieve data from the server side in asyncData before the component is mounted.在 nuxt 中,有一个在beforeCreate()之前运行的asyncData ,即您可以在安装组件之前在asyncData中从服务器端检索数据。

nuxt-生命周期

Alternatively, you can check on the created() for the process, eg或者,您可以检查进程的created() ,例如

created() {
  if (process.client) {
    // handle client side
  }
}


edit编辑

https://stackoverflow.com/a/53058870/2312051 https://stackoverflow.com/a/53058870/2312051

Audio.play() returns a Promise which is resolved when playback has been successfully started. Audio.play() 返回一个 Promise ,当播放成功开始时解决。 Failure to begin playback for any reason, such as permission issues, result in the promise being rejected.由于任何原因(例如权限问题)未能开始播放,都会导致 promise 被拒绝。

const playedPromise = video.play();
if (playedPromise) {
  playedPromise.catch((e) => {
    if (e.name === 'NotAllowedError' ||
        e.name === 'NotSupportedError') {
        //console.log(e.name);
    }
  });
}

In your case, looks like your browser/os does not allow automatic playing of audio.在您的情况下,您的浏览器/操作系统似乎不允许自动播放音频。 The user agent (browser) or operating system doesn't allow playback of media in the current context or situation.用户代理(浏览器)或操作系统不允许在当前上下文或情况下播放媒体。 This may happen, for example, if the browser requires the user to explicitly start media playback by clicking a "play" button.例如,如果浏览器要求用户通过单击“播放”按钮明确地开始媒体播放,这可能会发生。 Here is the reference. 是参考。

One possible solution would be to get the length of the Vue router entries.一种可能的解决方案是获取 Vue 路由器条目的长度。 This seems currently not to be possible without manually tracking.如果没有手动跟踪,目前这似乎是不可能的。 So I ended with this code in my layout component:所以我在布局组件中以这段代码结束:

watch: {
  $route () {
    Vue.prototype.$navigated = true
  }
}

The value of $navigated is undefined or true and so I know in the layout children components if video autoplay is possible or not. $navigated 的值是 undefined 或 true ,因此我在布局子组件中知道视频自动播放是否可能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NuxtJS:仅外部客户端插件/组件导致页面刷新错误 - NuxtJS: external client-side only plugin/component causes error on page refresh Vuejs 错误:客户端呈现的虚拟 DOM 树与服务器呈现的不匹配 - Vuejs Error: The client-side rendered virtual DOM tree is not matching server-rendered Nuxt3 如何知道在服务器端和客户端执行哪些代码? - How does Nuxt3 know which code to execute on the server-side vs client-side? 是否可以在 vuejs(客户端)中接受 POST 请求? - Is it possible to accept POST request in vuejs (client-side)? 忽略此错误 ==&gt; 客户端渲染的虚拟 DOM 树与服务器渲染的内容不匹配 - Ignore this error ==> The client-side rendered virtual DOM tree is not matching server-rendered content Nuxt.js 错误:客户端呈现的虚拟 DOM 树与服务器呈现的内容不匹配 - Nuxt.js error: The client-side rendered virtual DOM tree is not matching server-rendered content Nuxt客户端呈现的DOM与服务器呈现的内容不匹配 - Nuxt client-side rendered DOM not matching server-rendered content 客户端渲染的虚拟 DOM 树与服务器渲染的内容不匹配 - The client-side rendered virtual DOM tree is not matching server-rendered content VueRouter 的客户端授权如何安全? - How is client-side authorization with VueRouter safe? 如何仅在客户端在 Vue.js 3 中动态导入 CKEditor? - How to dynamically import CKEditor in Vue.js 3 only on client-side?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM