When I detect a specific width via class binding to make a mobile responsive web and swap between templates using display:none, the console keeps giving me an error.
I'm having trouble using 2 YouTube templates. If you use only one YouTube, the error disappears.

错误类型:ReferenceError:未定义YT.YouTube.vue?6574:118:25

如果替换我使用的类绑定模板有问题,还有其他方法吗?

<template>
  <div class="container">
    <div
      class="youtube_container"
      :class="{'youtube_container on' : WidthActive}">
      <h3 class="youtube_title">
        desktop video
      </h3>
    <!-- add -->
    <component 
       is="script" 
       id="youtube-iframe-js-api-script"
       src="https://www.youtube.com/iframe_api"
       />
      <YouTube
        src="https://youtu.be/GQmO52f26Ws"
        width="540" 
        height="360" />
    </div>
    <div
      class="youtube_container_mobile"
      :class="{'youtube_container_mobile on' : WidthmobileActive}">
      <h3 class="youtube_title">
        mobile video
      </h3>
      <YouTube
        src="https://youtu.be/GQmO52f26Ws"
        width="280" 
        height="300" 
        class="youtube_padding" />
    </div>
  </div>
</template>

<script setup>
import YouTube from 'vue3-youtube'
import { ref } from 'vue'

const WidthActive = ref(true)
const WidthPostion  = ref(0)
//mobile
const WidthmobileActive = ref(false)
const WidthmobilePostion  = ref(0)

window.addEventListener('resize', () => {
  WidthmobileActive.value = WidthmobilePostion.value > 590
  WidthmobilePostion.value = window.innerWidth
})
 window.addEventListener('resize', () => {
    WidthActive.value = WidthPostion.value < 591
    WidthPostion.value = window.innerWidth
})
</script>

<style>
.youtube_container{
  display: grid;
}
.youtube_container.on{
  display: none;
}
.youtube_container_mobile{
  display: grid;
}
.youtube_container_mobile.on{
  display: none;
}
</style>

推荐答案

更新

颠倒ref(false)ref(true)解决了这个问题.


这显然是一个已知的问题,也许可以try 一下that one

<component 
  is="script" 
  id="youtube-iframe-js-api-script"
  src="https://www.youtube.com/iframe_api"
/>

高于你的<YouTube>分.


或者试着给它两个不同的裁判,也就是ref="youtube"ref="youtube2",不确定.


否则,将以下内容添加到一个APEX文件中

<script id="youtube-iframe-js-api-script" src="https://www.youtube.com/iframe_api"></script>

Vue.js相关问答推荐

Vue3:如何在功能组件中重用插槽vnode

VueUse onClickOutside不支持右键单击

Vue 3 需要 main.js 中的文件

写入计算值时出错,但我没有写入任何值

在 Vuejs 中更新 Chartjs 图表数据集

使用计算(computed)属性更改视图

如何使用 vue 或 javascript 向服务器发送密码?

使用带有 v-date-picker 的 new Date() 不起作用

无法在 Vuex 中的其他操作中反跳操作

有条件地在 Vue 中添加一个 CSS 类

VueJs this.method 不是函数?如何在 Vuejs 的另一个方法中调用一个方法?

Vue:如何将 store 与组件一起使用?

Bootstrap-vue b-table,标题中带有过滤器

使用 vue-test-utils 配置过滤器

Leaflet+Vue+Vuetify / Leaflet map 隐藏 vuetify 弹出对话框

Vue.js 中的 CSS 框架

无法访问函数内的数据属性

在 Chrome DevTools 中实时更改时 destruct 了 Vue.js 应用程序(Webpack 模板)的页面样式

Vue 3 – 渲染无法动画的非元素根 node

如果通过 jquery 更新,Vuejs 绑定不起作用