如果元素存在,则实例Vue?

编程入门 行业动态 更新时间:2024-10-11 05:20:09
本文介绍了如果元素存在,则实例Vue?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在构建一个应用程序,其中一个页面有一些虚拟机,而另一些则没有.当我们从一个页面切换到另一个页面时显示以下警告:

I'm building an app that a page has some vms, in others not. When we change from one page to another show the following warning:

[Vue 警告]:找不到元素:#element-id

如何仅在页面上有元素的情况下实例化虚拟机并避免错误?

How do I instantiate VMs only if there is the element on the page and avoid the error?

注意:我正在使用 Rails 资产管道,因此,所有 javascript 都连接到一个文件中.

NOTE: I'm using Rails asset pipeline, so, all the javascript is concatenated into a single file.

推荐答案

保持原样没有问题.这只是一个仅在调试模式打开时运行的警告,因此无论如何您都应该在生产中将其关闭.

There are no issues just leaving it as is. This is just a warning that only runs when debug mode is on, so you should have it turned off in production anyway.

如果你想摆脱它,只需在启动Vue之前检查元素是否存在-

If you want to get rid of it, just check if the element exists before launching Vue -

if(document.getElementById("element-id")){
  new Vue({...})
}

Ref: 如何检查元素是否存在于可见的 DOM?

这篇关于如果元素存在,则实例Vue?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-27 02:13:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1146361.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实例   元素   Vue

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!