百变鹏仔-专注前端行业精选
vue中获取节点的两种方法,vue组件中通过mounted和操作dom获取节点操作的方法
作者:鹏仔先生日期:2020-06-01 16:07:06浏览:1530分类:JavaScript
在使用VUE时,不能直接对节点直接进行操作,所以可能大家需要操作dom获取节点或者通过mounted来对节点进行操作,那么鹏仔给大家带来下面两个方法讲解
第一种:通过选择器来获取
template中代码
<template> <div> <canvas id='top' > </div> </template>
js代码
<script> export default{ mounted(){ let top = document.querySelector("#top"); } } </script>
之前,我们使用document.getElementById来获取id,在VUE中,我们需要使用 document.querySelector 来获取。
第二种:使用ref获取
template中代码
<template> <div> <canvas ref='top' > </div> </template>
js代码
<script> export default{ mounted(){ let canvas=this.$refs.top; } } </script>
手机扫码访问
猜你还喜欢
- 04-19 VUE实现点击复制
- 04-16 vue将页面生成图片 vue生成海报
- 04-16 vue路由切换滑动效果 vue页面跳转交互 vue实现动画跳转
- 04-16 table固定表头和列 css实现表格固定表头
- 04-07 vue跳转页面清除历史记录,页面跳转删除历史记录
- 02-22 VUE You are using the runtime-only build of Vue where the template compiler is not available. Either
- 01-19 elementui多选上传 before-upload 格式效验错误总会触发before-remove (elementui多选上传on-success只执行了一次,只上传成功了一条)
- 10-08 vue按钮限制连点封装 自定义指令限制连点
- 09-14 nvm常用命令有哪些?nvm如何切换node版本?nvm如何下载node?
- 08-19 使用HBuilderX将vue或H5项目打包app
- 07-15 小程序嵌入网页向小程序跳转并传参,微信小程序中实现公众号授权获取openId
- 07-13 vue中实现文件批量打包压缩下载(以及下载跨域问题分析)
暂无评论,来添加一个吧。