vue3文本渲染,属性渲染,事件绑定命令

文本渲染命令

命令必须依赖标签,在开始标签中使用
命令支持字符串模板:`hello ${message}`
命令支持常见运算符、常见对象API调用、函数调用

v-text 指令:
可以使用 v-text 指令来替代插值表达式,如:
不支持识别html结构的文本

<p v-text="message"></p>

v-html 指令:
用于渲染包含 HTML 的内容,但要谨慎使用以避免 XSS 攻击,例如:

<div v-html="htmlContent"></div>
属性渲染命令

语法为v-bind:属性名=‘数据名’,可以简写成:属性名=‘数据名’

v-bind 指令:
可以使用 v-bind 指令来动态地绑定数据到元素的属性上,例如:

<a v-bind:href="url">Link</a>
事件绑定命令

我们使用v-on来监听DOM事件,并在事件触发时执行对应的Vue的js代码

用法:v-on:click="handler"或简写为@click="handler"
vue中的事件名=原生事件名去掉on前缀
handler的值可以是方法事件处理器,也可以是内联事件处理器
绑定事件时,可以通过一些绑定的修饰符

.once //只触发一次事件
.prevent //阻止默认事件
.stop //阻止事件冒泡
.capture //使用事件捕获模式而不是冒泡模式
.self //只在事件发送者自生触发时采触发事件
<!--事件绑定函数-->
<button @click="fun1()">hello</button>

<!--内联事件处理器-->
<!--import {ref} from 'vue';let counter=ref(3)-->
<button @click="counter++" >+</button>

<!--事件修饰符,只绑定1次-->
<button @click.once="counter++">+</button>
Vue实现富文本渲染有多种方法,以下为你介绍一些常见方式及示例代码: ### 使用quill-editor插件 在Vue项目中可以借助`quill-editor`插件实现富文本功能。以下是相关的Vue template代码: ```vue <template> <div> <!-- 富文本 --> <quill-editor class="editor" v-model="formData.contentData" :options="editorOption" @change="onEditorChange($event)"> <el-input type="textarea" auto-complete="off" :disabled="isChange"></el-input> </quill-editor> </div> </template> ``` 在上述代码里,`quill-editor`组件绑定了`formData.contentData`数据,并且可通过`editorOption`配置编辑器选项,同时监听`change`事件来处理内容变更[^1]。 ### 自定义富文本组件 可以创建自定义的富文本组件并在其他组件中使用。以下是组件使用方式示例: ```vue <rich-text v-model="details" ref="fueditModule" @loadingCompleted="loadingCompleted"></rich-text> export default Vue.extend({ name: &#39;UpdateText&#39;, components: { RichText: () => import(&#39;@/components/RichText.vue&#39;), }, methods: { // 富文本组件加载完成回调 loadingCompleted() { try { console.log(&#39;editor加载完成,回调父组件&#39;); // this.details = this.$refs.fueditModule.setHtml(&#39;<p><b>招商会详情!!</b></p>&#39;); } catch (error) { console.log(&#39;打开弹窗 err =&#39;, error); } }, // 调用子组件获取富文本内容 this.details = this.$refs.fueditModule.getHtml(); // 调用子组件设置富文本内容 this.$refs.fueditModule.setHtml(&#39;<p><b>设置详情!!</b></p>&#39;); // 调用子组件销毁富文本编辑框 this.$refs.fueditModule.beforeDestroy(); } }) ``` 这里通过`rich-text`组件实现富文本功能,同时在父组件中可以调用子组件的方法来获取、设置内容以及销毁编辑框[^2]。 ### 使用Vue3的QuillEditor组件 在Vue3项目中,可使用`@vueup/vue-quill`库中的`QuillEditor`组件。示例代码如下: ```vue <template> <QuillEditor v-model:content="content" contentType="html" theme="snow" toolbar="full" /> </template> <script> import { QuillEditor } from &#39;@vueup/vue-quill&#39; import &#39;@vueup/vue-quill/dist/vue-quill.snow.css&#39; export default { components: { QuillEditor }, data() { return { content: &#39;<h1>初始内容</h1>&#39; } } } </script> ``` 此组件具有轻量级、API简洁、支持自定义模块等特点,可通过`v-model:content`绑定内容数据,还能设置内容类型、主题和工具栏等选项[^3]。 ### 直接在HTML中渲染文本前端Vue项目里,也能直接在HTML中渲染文本。示例代码如下: ```vue <template> <div class="ArticleDetail"> <div ref="editor" style="text-align:left"></div> </div> </template> ``` 不过这种方式通常需要额外的逻辑来处理富文本内容渲染和更新[^4]。 ### 另一种quill-editor使用方式 以下是另一个使用`quill-editor`的示例: ```vue <template> <!-- 富文本 --> <quill-editor ref="myQuillEditor" v-model="content" :options="editorOption" @blur="onEditorBlur($event)" @focus="onEditorFocus($event)" @ready="onEditorReady($event)" @change="onEditorChange($event)" /> </template> ``` 该示例中,`quill-editor`组件绑定了`content`数据,同时监听了多个事件,如`blur`、`focus`、`ready`和`change`事件[^5]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值