34 在Vue3中如何通过ref获取DOM元素

概述

通过ref获取DOM节点也是一种非常常见的用法。

最常见的地方就是表单提交的时候,我们经常会给表单绑定一个ref,在条件之前通过ref获取表单数据,并校验表单数据是否正确。

在本节课中,我们使用ref引用一个textarea文本域,同事实记录文本域中的段落数量,句子数量和单词数量。

本节课需要依赖一个第三方库,我们先安装:

bash 复制代码
yarn add countable

基本用法

我们创建src/components/Demo34.vue,代码如下:

html 复制代码
<script setup>
import * as Countable from 'countable'
import {onMounted, ref} from "vue";

const count = ref({})
const textAreaRef = ref(null)

onMounted(() => {
  Countable.on(textAreaRef.value, (value) => {
    count.value = value
  })
})
</script>
<template>
  <!--段落-->
  <textarea
      ref="textAreaRef"
      cols="50"
      rows="7"
  >
    </textarea>

  <!--段落计数-->
  <ul>
    <li>段落: {{ count.paragraphs }}</li>
    <li>句子: {{ count.sentences }}</li>
    <li>单词: {{ count.words }}</li>
  </ul>
</template>

接着,我们修改src/App.vue:

html 复制代码
<script setup>
import Demo from "./components/Demo34.vue"
</script>
<template>
  <h1>欢迎跟着Python私教一起学习Vue3入门课程</h1>
  <hr>
  <demo/>
</template>

然后,我们浏览器访问:http://localhost:5173/

完整代码

package.json

json 复制代码
{
  "name": "hello",
  "private": true,
  "version": "0.0.0",
  "type": "module",
  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "preview": "vite preview"
  },
  "dependencies": {
    "countable": "^3.0.1",
    "vue": "^3.3.8"
  },
  "devDependencies": {
    "@vitejs/plugin-vue": "^4.5.0",
    "vite": "^5.0.0"
  }
}

vite.config.js

js 复制代码
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

export default defineConfig({
  plugins: [vue()],
})

index.html

html 复制代码
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <link rel="icon" type="image/svg+xml" href="/vite.svg" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Vite + Vue</title>
  </head>
  <body>
    <div id="app"></div>
    <script type="module" src="/src/main.js"></script>
  </body>
</html>

src/main.js

js 复制代码
import { createApp } from 'vue'
import App from './App.vue'

createApp(App).mount('#app')

src/App.vue

html 复制代码
<script setup>
import Demo from "./components/Demo34.vue"
</script>
<template>
  <h1>欢迎跟着Python私教一起学习Vue3入门课程</h1>
  <hr>
  <demo/>
</template>

src/components/Demo34.vue

html 复制代码
<script setup>
import * as Countable from 'countable'
import {onMounted, ref} from "vue";

const count = ref({})
const textAreaRef = ref(null)

onMounted(() => {
  Countable.on(textAreaRef.value, (value) => {
    count.value = value
  })
})
</script>
<template>
  <!--段落-->
  <textarea
      ref="textAreaRef"
      cols="50"
      rows="7"
  >
    </textarea>

  <!--段落计数-->
  <ul>
    <li>段落: {{ count.paragraphs }}</li>
    <li>句子: {{ count.sentences }}</li>
    <li>单词: {{ count.words }}</li>
  </ul>
</template>

启动方式

bash 复制代码
yarn
yarn dev

浏览器访问:http://localhost:5173/

相关推荐
zhangxingchao21 分钟前
Android开发者如何快速上手Flutter开发
前端
空&白36 分钟前
css元素的after制作斜向的删除线
前端·css
海盐泡泡龟37 分钟前
“组件、路由懒加载”,在 Vue3 和 React 中分别如何实现? (copy)
前端·javascript·react.js
coding随想1 小时前
深入浅出WebGL:在浏览器中解锁3D世界的魔法钥匙
javascript
_揽1 小时前
html如何在一张图片上的某一个区域做到点击事件
前端·html
踢足球的,程序猿1 小时前
从 Vue 2.0 进阶到 Vue 3.0 的核心技术解析指南
前端·javascript·vue.js·前端框架·html
冷凌爱1 小时前
Fetch与Axios:区别、联系、优缺点及使用差异
前端·node.js·js
袁煦丞2 小时前
跨平台终端王者Tabby:cpolar内网穿透实验室第632个成功挑战
前端·程序员·远程工作
Sailing2 小时前
Grafana-mcp-analyzer:基于 MCP 的轻量 AI 分析监控图表的运维神器!
前端·node.js·mcp
阿山同学.2 小时前
AWS 亚马逊 S3存储桶直传 前端demo 复制即可使用
前端·javascript·aws