使用v-model绑定文本域则属于不太常见的情况了,因为文本域一般会被富文本替代,而富文本的话一般又会引入一些第三方的富文本库,所以在真实的开发中,使用v-model绑定文本域的场景实际上并不多见。
这里,我们了解一下v-model绑定文本域的基本用法,熟悉一下用法即可。
我们创建src/components/Demo18.vue,在这个组件中,我们要:
代码如下:
<script setup>
import {ref} from "vue";
const content = ref("")
script>
<template>
<div>
<textarea v-model="content" rows="3"/>
div>
<div>
<h3>内容h3>
<p>{{ content }}p>
div>
template>
接着,我们修改src/App.vue,引入Demo18.vue并进行渲染:
<script setup>
import Demo from "./components/Demo18.vue"
script>
<template>
<h1>欢迎跟着Python私教一起学习Vue3入门课程h1>
<hr>
<Demo/>
template>
然后,我们浏览器访问:http://localhost:5173/
{
"name": "hello",
"private": true,
"version": "0.1.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build"
},
"dependencies": {
"vue": "^3.3.8"
},
"devDependencies": {
"@vitejs/plugin-vue": "^4.5.0",
"vite": "^5.0.0"
}
}
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
export default defineConfig({
plugins: [vue()],
})
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 + Vuetitle>
head>
<body>
<div id="app">div>
<script type="module" src="/src/main.js">script>
body>
html>
import { createApp } from 'vue'
import App from './App.vue'
createApp(App).mount('#app')
<script setup>
import Demo from "./components/Demo18.vue"
script>
<template>
<h1>欢迎跟着Python私教一起学习Vue3入门课程h1>
<hr>
<Demo/>
template>
<script setup>
import {ref} from "vue";
const content = ref("")
script>
<template>
<div>
<textarea v-model="content" rows="3"/>
div>
<div>
<h3>内容h3>
<p>{{ content }}p>
div>
template>
yarn
yarn dev
浏览器访问:http://localhost:5173/