位置: IT常识 - 正文

vue3.2 基础及常用方法(vue3.0用法)

编辑:rootadmin
vue3.2 基础及常用方法

推荐整理分享vue3.2 基础及常用方法(vue3.0用法),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:vue3快速入门,vue 3教程,vue基本,vue 3教程,vue 3教程,vue3 入门,vue基础知识,vue3.0用法,内容如对您有帮助,希望把文章链接给更多的朋友!

Vue3.2(21年8月10日)相比于Vue3新增了语法糖,减少了代码冗余

Vue3相比于Vue2,在虚拟DOM,编译, 数据代理,打包构建封面进行了优化

Vue3使用组合式API, 适合大型项目, 去除了this

vue2的 beforeCreate 和 created 被新增的setup生命周期替代

vue3 使用插件: volar 配置用户代码片段可以快速输入vue3 模板

1. css支持v-bind 指令:<template> <div class="box">{{color}}</div></template><script setup> import {ref} from 'vue' let color = ref('red')</script><style scoped>.box { width: 100px; height: 50px; background-color: v-bind(color);}</style>2. setup语法糖

vue3.0的变量需要return出来才可以在template中使用, 写法冗余

vue3.2 在script标签中添加setup解决问题 组件只需要引入,不需要注册,属性方法不需要返回,不需要写setup函数,不需要写export default

3. data定义3.1 直接定义无响应式

let name = ‘zhangsan’

3.2 ref定义基本数据类型

在script标签内,需要用 name.value 修改和获取值

可以接受基本类型、也可以是复杂类型(比如对象,数组)。建议处理基本类型数据。

基本类型的数据:响应式依然是靠Object.defineProperty()的get与set完成的。

<template> <div>{{name}}</div> <button @click="setName">set</button></template><script setup> import {ref} from 'vue' let name = ref('zhangsan') const setName = () => { name.value = 'lisi' }</script><style scoped></style>3.3 reactive 定义引用数据类型

reactive支持引用类型, 直接 变量.属性 使用和修改

<template> <div>{{user.name}}</div> <div>{{user.money}}</div> <button @click="setItem">set</button></template><script setup> import {reactive} from 'vue' let user = reactive({ name: 'zhangsan', money: 1000 }) const setItem = () => { user.money = 1500 user.gupiao = 100 }</script><style scoped></style>4. methods方法定义

在template中 :

<button @click='addMoney'>加钱</button>

在script中:

const money = ref(1000)const addMoney = () => { money.value ++}5. computed计算属性

获得一个新的属性

<template> <div>{{nameString}}</div> <div>{{user.money}}</div> <button @click="setItem">set</button></template><script setup> import {reactive, computed} from 'vue' let user = reactive({ name: 'zhangsan', money: 1000 }) const nameString = computed(() => { return '我的名字是' + user.name })</script><style scoped></style>6 watch使用:

监听响应式数据的变化

watch(数据源, 执行函数, [配置参数])// 配置的参数:立即执行, 深度监听{immediate: true, deep: true}

6.1 监听基本数据类型 单一数据源

<script setup>import {ref, watch} from 'vue' let name = ref('zhnagsan') //直接监听属性watch(name,(newVal,oldVal)=>{ console.log('变量发生了改变...',newVal, oldVal);})

6.2 监听引用数据类型 单一数据源

<template> <div>{{user.name}}</div> <div>{{user.money}}</div> <button @click="setItem">set</button></template><script setup> import {reactive, watch} from 'vue' let user = reactive({ name: 'zhangsan', money: 1000 }) // 监听方法返回的属性 watch(() => user.money, (newVal, oldVal) => { console.log('user money 变了: ', newVal, oldVal) }) const setItem = () => { user.money = 1500 }</script>

6.2 监听引用数据类型 多数据源[深度监听]

<template> <div>{{user.name}}</div> <div>{{user.money}}</div> <div>{{user.hobby.study}}</div> <button @click="setItem">set</button></template><script setup> import {reactive, watch} from 'vue' let user = reactive({ name: 'zhangsan', money: 1000, hobby: { study: '语文' } }) // 深度监听 {deep:true} watch(() => user.hobby.study, (newVal, oldVal) => { console.log('user money 变了: ', newVal, oldVal) }, {deep:true}) const setItem = () => { user.hobby.study = '数学' }</script>7. 生命周期vue3.2 基础及常用方法(vue3.0用法)

import { onMounted } from 'vue'onMounted(() => { console.log(document.querySelector('.box')) // 可以获取DOM})7.1 ref获取元素<template> <div ref="box"> <button>Hehe</button> </div></template><script setup> import { ref, onMounted } from "vue"; const box = ref(null); onMounted(() => { console.log(box.value); });8 组件使用创建 components/Son.vue在App.vue 中导入子组件Vue3.2 在导入子组件时,自动注册该组件组件名格式: 大驼峰写法

子组件Son.vue

<template> <div>Son compoment</div></template><script setup></script>

父组件

<template> <Son/></template><script setup>import Son from './components/Son.vue'</script>

全局组件: main.js

app.component('ComponentB', ComponentB)<ComponentA/>9.组件通信9.1 父传子 defineProps

子组件Son.vue

<template> <div>Son compoment</div> <div>{{name}}</div> <div>{{like}}</div></template><script setup>const props = defineProps({ name: { type: String, default: "" }, like: { type: Array, default: () => [] }})</script>

父组件

<template> <Son name="小灰灰" :like="like"/></template><script setup>import Son from './components/Son.vue'let like = ["红太狼", "灰太狼"]</script>9.2 子传父 defineEmits

子组件

<template> <button @click="sendData">传递数据</button></template><script setup>// 自定义事件const emit = defineEmits(['send'])// 事件执行函数const sendData = () => { emit('send', '子组件的数据')}</script>

父组件

<template> <Son @send="getData"/></template><script setup>import Son from './components/Son.vue'const getData = (data) => { console.log('父组件获取到: ', data)}</script>条件渲染<template> <div v-if="type === 'A'"> A </div> <div v-else-if="type === 'B'"> B </div> <div v-else> Not A/B </div></template><script setup>let type = 'B'</script>列表渲染<template> <li v-for="item in items"> {{ item.message }} </li></template><script setup>import {ref} from 'vue'let items = ref([ {message: 'm1'}, {message: 'm2'}, {message: 'm3'}])setTimeout(() => { items.value.push({message: 'm4'})}, 2000)</script>事件处理<button @click="say">Say bye</button>// 传递参数<button @click="say('bye')">Say bye</button>

事件修饰符:

.stop .prevent .self .capture .once .passive

<!-- 单击事件将停止传递 --><a @click.stop="doThis"></a>表单输入绑定(用于、、)<input v-model="text"><input type="radio" v-model="pick" :value="first" /><input type="radio" v-model="pick" :value="second" />插槽slot 实现 可以用在不同的地方渲染各异的内容,但同时还保证都具有相同的样式。

使用

<FancyButton> Click me! <!-- 插槽内容 --></FancyButton>

组件:

<button class="fancy-btn"> <slot></slot> <!-- 插槽出口 --></button>

元素是插槽, 父元素插槽内容将在slot处渲染 渲染之后的DOM:

<button class="fancy-btn">Click me!</button>

另一中js的方式理解:将内容传递给子元素, 子元素包裹时候生成DOM,返回给父元素

// 父元素传入插槽内容FancyButton('Click me!')// FancyButton 在自己的模板中渲染插槽内容function FancyButton(slotContent) { return `<button class="fancy-btn"> ${slotContent} </button>`}

子组件

<template> <li v-for="item in items"> <slot></slot> </li> <button class="fancy-btn"> <slot></slot> <!-- 插槽出口 --> </button></template><script setup>import {ref} from 'vue'let items = ref([ {message: 'm1'}, {message: 'm2'}, {message: 'm3'}])</script>

父组件

<Son> ABC </Son>Teleport: 组件传送到DOM节点 <Teleport> 是一个内置组件,它可以将一个组件内部的一部分模板“传送”到父组件的外的其他 DOM 结构外层的位置去。

如下传送到了body元素上

<template> <div> <button @click="open = true">Open Modal</button> <Teleport to="body"> <div v-if="open" class="modal"> <p>Hello from the modal!</p> <button @click="open = false">Close</button> </div> </Teleport> </div></template><script setup>import { ref } from 'vue'const open = ref(false)</script><style scoped>.modal { position: fixed; z-index: 999; top: 20%; left: 50%; width: 300px; margin-left: -150px; border: 2px red solid;}</style>

// 传送到id为teleport-target的DOM元素上:

<Teleport to="#teleport-target"> </Teleport>模板引用 通过ref获取DOM元素<template> <div ref="divRef">divRef</div></template><script> import { onMounted, ref } from 'vue' const divRef = ref(null) onMounted(() => { // 挂载后才可以获取DOM元素 console.log('[long] divRef: ', divRef.value) })</script>条件渲染 17.1 条件渲染 v-if<div v-if="type==='A'">A</div><div v-else-if="type==='B'">B</div><div v-else>C</div>

多元素条件渲染:使用<template>包装器元素

<template v-if="ok"> <p>P1</p> <p>P2</p></template>

17.2 条件渲染 v-show v-if 不保留DOM元素,切换开销更高 v-show 保留DOM元素,设置display属性,不支持template包装器元素,初始渲染开销高

17.3 不推荐使用v-if 和 v-for同时使用, 同时使用时v-if首先执行

列表渲染v-forconst items = ref([{ message: 'F1' },{ message: 'F2' }])<li v-for="(item, index ) in items"> {{ item.message }} - {{ index }}</li>

item 是迭代项别名

使用template渲染多个元素

<ul> <template v-for="item in items"> <li>{{ item.msg }}</li> <li class="divider" role="presentation"></li> </template></ul>
本文链接地址:https://www.jiuchutong.com/zhishi/299352.html 转载请保留说明!

上一篇:前端面试中经常提到的LRU缓存策略详解(前端面试经常被问的问题)

下一篇:Node.js下载安装及环境配置教程【超详细】(node.js安装步骤)

  • 职工薪酬支出及纳税调整
  • 三代税款手续费退费申请
  • 门诊医保报销发票
  • 小规模的开专票怎么征税
  • 小型微利企业预缴
  • 资产减值损失在利润表怎么填列
  • 小企业会计准则会计科目表
  • 蔬菜批发销售免税政策
  • 民办幼儿园提供什么服务
  • 损益类算费用吗
  • 土地结转会计分录是什么
  • 公务出差乘坐飞机
  • 机动车发票红冲可以作废吗
  • 服务类的企业发票税率
  • 陈列费发票属于什么类型
  • 发票最大限额999元,超过了怎么办
  • 税率是3%开成5%怎么办
  • 关于固定资产管理
  • 对公账户提取备用金可以每天提现嘛
  • 直接收费金融服务、提供贷款服务和金融商品转让税率
  • 期末未缴税额是负数用于抵扣如何填报
  • 绿植租赁公司都适合什么名字
  • 房地产公司需要和哪些部门打交道
  • 对公提回贷算收入吗
  • 代缴个税税务局奖励款怎么做账?
  • 国税申报纳税调整项目怎么填的?
  • 知识产权代理公司怎么盈利
  • 未摊销金额怎么算
  • 固定资产折旧计算方法
  • 销售折扣收货方法有哪些
  • 苹果手机怎么刷机
  • 异地提供建筑服务预缴增值税
  • 仓储费用结算方式有哪些
  • php fopen ftp文件不存在
  • yolov8目标检测测试输出类别个数
  • tf fans club
  • nodejs安装及环境配置win7
  • 理财产品利息计算方法
  • 筹办期间发生的广告费和业务宣传费可以扣除吗
  • 帝国cms修改提示怎么设置
  • 违约拒绝赔偿怎么办
  • java sc
  • Win2008中SqlServer2008 无法打开错误日志文件导致无法启动的解决方法
  • 建筑安装工程公司起名
  • 个人所得税的计算标准
  • sql server临时表的生命周期
  • 企业清算是怎么做账的
  • 合并报表抵消分录的基本原理
  • 公司法人必须在公司名下缴纳社保吗
  • 生产部的管理人员算什么费用
  • 以前年度损益调整怎么做账
  • 培训学校的主营业务是什么
  • 什么是残保金减免政策
  • 企业职工充值地税怎么交
  • 注册资本金印花税什么时候缴纳
  • 进出口企业需要哪些手续
  • 固定资产净残值最后怎么处理
  • 会计建账的内容
  • 基于Sql Server通用分页存储过程的解决方法
  • mysql57服务无法启动,找不到文件夹
  • bios设置第一启动项图解
  • 如何恢复ubuntu
  • 如何设置无线网密码
  • ic_launcher.xml
  • win7系统更新怎么关闭
  • windows8启动项设置
  • linux必会的20种服务
  • 游戏引擎官方平台
  • ghost详细说明
  • android方法大全
  • 关于android工程中的assets目录的正确说法
  • 浅谈jQuery中ajaxPrefilter的应用
  • 海关编码查询 商品编码查询
  • 小规模纳税人租金收入不超过30万
  • 12366纳税服务热线存在的问题
  • 环保税征税范围44项
  • 北京第六税务所电话号码
  • 企业所得税一年算几次
  • 沈阳沈河区税务局待遇
  • 税票电话号码变更影响抵扣吗
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设