前端学习--Vue3.0(1)

news2024/9/16 23:28:54

1使用create-vue搭建Vue3项目

1.1 认识create-vue

create-vue是Vue官方新的脚手架工具,底层切换到了 vite(下一代前端工具链),为开发提供极速响应

create-vue基于vite

vue-cli基于webpack

1.2 创建项目

需要16.0及以上的node.js 

npm init vue@latest 创建项目

 npm i 安装依赖包

npm run dev 运行项目

打开浏览器输入地址得到:

 1.3 熟悉项目目录

2 组合式API

2.1 setup选项

        --组合式API的入口

执行时机:优先于beforeCreate()

<script>
export default{
  setup(){
    const message = 'this is message'
    //vue3中没有this对象
    const logMessage = () => {
      console.log(message)
    }
    //如果要在模板中使用setup的数据 必须return
    return{
      message,
      logMessage
    }
  },
  beforeCreate(){
    console.log('beforeCreate')
  }
}
</script>

<template>
  <div>
    {{ message }}
    <button @click="logMessage"> log </button>
  </div>
</template>

setup语法糖

        --更简易的写法

<script setup>
const message = 'this is message'
    const logMessage = () => {
      console.log(message)
    }
</script>
<template>
  <div>
    {{ message }}
    <button @click="logMessage"> log </button>
  </div>
</template>

2.2 reactive和ref

reactive()

接受对象类型数据的参数传入并返回一个响应式的对象

<script setup>
//导入reactive
import { reactive } from 'vue'

//reactive接收的参数必须是对象
const state = reactive({
  count:0
})

const setCount = () => {
  state.count++
}
</script>

<template>
  <div>
    <button @click="setCount">{{ state.count }}</button>
  </div>

</template>

ref()

接收简单类型或者对象类型的数据传入并返回一个响应式的对象

<script setup>
  import {ref} from 'vue'

  const count = ref(0)
  const setCount = () => {
    //脚本区域修改ref产生的响应式数据必须通过.value访问
    count.value ++
  }
</script>
<template>
  <div>
    <button @click="setCount">{{ count }}</button>
  </div>

</template>

2.3 计算属性函数computed()

<script setup>
import { ref, computed } from 'vue'
const list = ref([1, 2, 3, 4, 5, 6, 7])
const computedList = computed(() => {
  return list.value.filter(item => item > 2)
})

setTimeout(() => {
  list.value.push(9, 10)
},2000)
</script>

<template>
  <div>
    原始数组--{{ list }}
  </div>
  <div>
    计算属性数组-- {{ computedList }}
  </div>
</template>

2.4 watch函数

侦听单个数据

<script setup>
import { ref, watch } from 'vue'
const count = ref(0)
const setCount = () => {
  count.value ++
}

//侦听的数据源,该数据源发生变化时执行的回调函数
//这里ref对象不需要加.value
watch(count, (newVal, oldVal) => {
  console.log('count变化了',newVal, oldVal)
})
</script>

<template>
  <div>
    <button @click="setCount">{{ count }}</button>
  </div>
</template>

侦听多个数据变化

<script setup>
import { ref, watch } from 'vue'
const count = ref(0)
const setCount = () => {
  count.value ++
}

const name = ref('ab')
const setName = () => {
  name.value += 'c'
}

watch([count, name],
([newCount, newName], [oldCount, oldName]) => {
  console.log('变化了',newCount, newName, oldCount, oldName)
})
</script>

<template>
  <div>
    <button @click="setCount">修改{{ count }}</button>
    <button @click="setName">修改{{ name }}</button>
  </div>
</template>

immediate参数

数据创建时首先执行一次

watch(count, (newval, oldval) => {
  console.log('count变化了')
},{
  immediate:true
})

deep参数

watch监听的ref对象默认浅层侦听,直接修改嵌套的对象属性不会触发回调执行,需要开启deep选项

<script setup>
import { ref, watch, reactive } from 'vue'
const count = ref({
  age:0
})
const setCount = () => {
  count.value.age ++
}


watch(count, (newval, oldval) => {
  console.log('count变化了')
}, {
  deep:true
})
</script>

<template>
  <div>
    <button @click="setCount">修改{{ count.age }}</button>
  </div>
</template>

deep会产生性能损耗,不推荐开启 

其实我觉得直接用reactive声明对象不就行了嘛...

精确侦听对象中某个属性变化

<script setup>
import { ref, watch, reactive } from 'vue'
const stu = ref({
  name:'zs',
  age:0
})
const addAge = () => {
  stu.value.age ++
}
const setName = () =>{
  stu.value.name = 'ls'
}


watch(
  () => stu.value.age ,
  () => {
  console.log('stu变化了') //只有当age变化才会执行
})
</script>

<template>
  <div>
    <button @click="addAge">修改age{{ stu.age }}</button>
    <button @click="setName">修改name{{ stu.name }}</button>
  </div>
</template>

3 生命周期函数

 onBeforeMount()

beforeMount()

在这一步中,根元素还不存在。在选项API中,可以使用this.$els来访问

// 选项 API
export default {
   beforeMount() {
     console.log(this.$el)
   }
 }

onBeforeMount()

在组合API中,没有this,必须在根元素上使用ref,此时输出为null

// 组合 API
<template>
  <div ref='root'>
    home page
  </div>
</template> 

<script>
import { ref, onBeforeMount } from 'vue'

export default {
  setup() {
     const root = ref(null) 
     onBeforeMount(() => {   
        console.log(root.value) 
     }) 
     return { 
        root
     }
   },
   beforeMount() {
     console.log(this.$el)
   }
}
</script>

onMounted()

在组件的第一次渲染后调用,该元素现在可用,允许直接DOM访问

在 选项API中,我们可以使用this.$el来访问我们的DOM,在组合API中,我们需要使用refs来访问Vue生命周期钩子中的DOM。

<template>
  <div ref='root'>
    home page
  </div>
</template> 

<script>
import { ref, onMounted } from 'vue'
 export default {
   setup() { 
     const root = ref(null)
     onMounted(() => {
       console.log(root.value)
     })
     return {
       root
     }
   },
   mounted() { 
     console.log(this.$el)
   }
 } 
</script>

onBeforeUpdate()&onUpdated()

beforeUpdate()&updated()

<template>
    <div>
      <p>{{val}} | edited {{ count }} times</p>
      <button @click='val = Math.random(0, 100)'>Click to Change</button>
    </div>
 </template> 


<script>
export default {
   data() {
      return {
        val: 0
      }
   },
   beforeUpdate() {
      console.log("beforeUpdate() val: " + this.val)
   },
   updated() {
      console.log("updated() val: " + this.val
   }
 } 
</script>

onBeforeUpdate()&onUpdated()

<template>
  <div>
    <p>{{val}} | edited {{ count }} times</p>
    <button @click='val = Math.random(0, 100)'>Click to Change</button>
  </div>
</template> 
<script>
import { ref, onBeforeUpdate, onUpdated } from 'vue'
 
 export default {
   setup () {
     const count = ref(0)
     const val = ref(0)
 
     onBeforeUpdate(() => {
       count.value++;
       console.log("beforeUpdate");
     })
 
     onUpdated(() => {
       console.log("updated() val: " + val.value)
     })
 
     return {
       count, val
     }
   }
 }
</script>

参考:(99条消息) Vue 3 生命周期完整指南_vue3生命周期_@大迁世界的博客-CSDN博客

4 父子通信

4.1 父传子

father.vue

<script setup>
//setup语法糖下导入组件不需要注册直接可以使用
import son from './son.vue'
import {ref} from 'vue'
const count = ref(100)
setTimeout(() => {
    count.value = 200
},3000)

</script>

<template>
  <div class="father">
    <h2>father组件</h2>
    <son :count="count" message="你爸爸的消息"></son>
  </div>
</template>

son.vue

<script setup>
const props = defineProps({
    message:String,
    count:Number
})
</script>

<template>
  <div class="son">
    <h2>son组件</h2>
    <div>
        这是fatherMessage -- {{ message }}--{{ count }}
    </div>
  </div>
</template> 

4.2 子传父

son.vue

<script setup>
const emit = defineEmits(['get-message'])
const sendMsg = () =>{
    emit('get-message','this is son message')
}
</script>

<template>
  <div class="son">
    <h2>son组件</h2>
    <div>
        <button @click="sendMsg">发送消息</button>
    </div>
  </div>
</template> 

father.vue

<script setup>
import son from './son.vue'
import {ref} from 'vue'
const fatherMsg = ref('')
const getMessage = (msg) => {
    fatherMsg.value = msg
}

</script>

<template>
  <div class="father">
    <h2>father组件--{{ fatherMsg }}</h2>
    <son @get-message="getMessage"></son>
  </div>
</template>

5 模板引用

通过ref标识获取真实的dom对象或者组件实例对象

<script setup>
import { onMounted, ref} from 'vue'
import test from './components/test.vue'

const title = ref(null)
const testcom = ref(null)

onMounted(() => {
  console.log(title.value)
  console.log(testcom.value)
})

</script>

<template>
  <h1 ref="title">我是h1</h1>
  <test ref="testcom"></test>
</template>

defineExpose()

默认情况下在<script setup>语法糖下组件内部的属性和方法不能被父组件直接获取

defineExpose()编译宏指定哪些属性和方法允许访问

6 provide和inject

 顶层组件向任意的底层组件传递数据和方法,实现跨层组件通信

跨层传递数据

father.vue

<script setup>
import son from './son.vue'
import { provide, ref } from 'vue'

provide('data-key', 'this is father data')

//如果要传递响应式数据,把第二个参数改为ref对象即可
const count = ref(0)
provide('count-key', count)

const countAdd = () => {
  count.value ++
}
</script>

<template>
  <div class="father">
    <h2>father组件</h2>
    <button @click="countAdd">+count</button>
    <son></son>
  </div>
</template>

son.vue

<script setup>
import grandson from './grandson.vue'
</script>

<template>
  <div class="son">
    <h2>son组件</h2>
    <grandson></grandson>
  </div>
</template> 

grandson.vue

<script setup>
import {inject} from 'vue'
const data = inject('data-key')
const count = inject('count-key')

</script>
<template>
  <h3>grandson组件</h3>
  <div>father来的数据为-- {{ data }} --{{ count }}</div>
</template>

跨层传递方法

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/617270.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

机器学习笔记:深度学习模型复杂度

1 时间复杂度&#xff08;模型运算次数&#xff09; 决定了模型的训练/预测时间用FLOPs指代 机器学习笔记&#xff1a;FLOPs_flop 机器学习_UQI-LIUWJ的博客-CSDN博客 2 空间复杂度&#xff08;模型参数数量&#xff09; 决定了模型的参数数量 2.1 全连接层参数量 包含bias…

EBU6304 Software Engineering 知识点总结_6 项目管理_下 Design Patterns

Design Patterns Decorator Design pattern 用于扩展系统功能的装饰模式。 比如我们有一个鸭子类&#xff0c;可以调用其鸭子叫的方法。我们初始化了一个鸭子对象&#xff0c;如何统计这个对象叫了几次&#xff0c;也就是其鸭子叫方法被调用了几次&#xff1f;记住OCP原则不…

mysql 在 linux下的安装 和 配置

文章目录 1. linux 安装mysql1. 源码安装1.找到源码包2&#xff1a; 进行配置3&#xff1a;初始化数据目录官方说明 自用4&#xff1a;启动数据库5:写入环境变量 2&#xff1a;使用yum安装1. 直接配置mysql仓库 或者下载MySQL Yum Repository2&#xff1a;安装启动进入mysql 3&…

整理推荐 6 个超好用的平面设计网站!

本文将为大家介绍了 6 个最佳学习平面设计的网站&#xff0c;包括即时设计、Behance、Awwwards、Dribbble、Designinspiration 和 Pinterest。选择学习平面设计网站需要考虑匹配自己的设计风格、是否具有局限性等&#xff0c;可以试用几个网站后选择最合适的。 1、即时设计 即…

我们投出去的简历为什么已读不回?来看面试官如何筛选简历、挑选求职者?

目录 前言&#xff1a; 应届生 1、看学历 2、看实习经历 3、看专业 职场人士 面试官喜欢问什么问题 总结 前言&#xff1a; 金三银四&#xff0c;是求职者蠢蠢欲动的季节&#xff0c;亦是企业摩拳擦掌的季节。 因为作为企业的一名金牌面试官&#xff0c;我收到的内推…

RK1126 C++ yolov5 6.2

基于 rk npu &#xff0c; 实现 yolov5 6.2 模型推理 实现过程 ⚡️​ 编译 opencv 需根据自己路径修改. cmake -D CMAKE_BUILD_TYPERELEASE \-D CMAKE_C_COMPILER./gcc-arm-8.3-2019.02-x86_64-arm-linux-gnueabihf/bin/arm-linux-gnueabihf-gcc \-D CMAKE_CXX_COMPILER./gc…

【Linux】多线程02 --- 线程的同步互斥问题及生产消费模型

&#x1f34e;作者&#xff1a;阿润菜菜 &#x1f4d6;专栏&#xff1a;Linux系统编程 目录 一、线程互斥1. 为什么要有共享资源临界保护&#xff1f; 2.理解加锁2.1 认识锁&#xff0c;使用锁 线程同步互斥问题是指多线程程序中&#xff0c;如何保证共享资源的正确访问和线程间…

阿里P8大佬七天七夜制作这份自动化核心知识点,错过了就是错过了

整理了一份自动化核心知识点。覆盖了web前端基础&#xff0c;HTML标签&#xff0c;CSS样式&#xff0c;自动化测试工具&#xff0c;webdriver环境搭建&#xff0c;元素定位&#xff0c;手机操作系统&#xff0c;移动自动化测试工具&#xff0c;自动化测试的流程与分类&#xff…

web自动化测试入门篇01——框架介绍

1. 目的 web自动化测试作为软件自动化测试领域中绕不过去的一个“香饽饽”&#xff0c;通常都会作为广大测试从业者的首选学习对象&#xff0c;相较于C/S架构的自动化来说&#xff0c;B/S有着其无法忽视的诸多优势&#xff0c;从行业发展趋、研发模式特点、测试工具支持&#x…

高完整性系统(7)Formal Verification and Validation

文章目录 Specification Process 规格化过程State Invariants案例check ... expect Alloy是一种用于构建和检查抽象模型的语言和工具。当Alloy说所有断言都成立时&#xff0c;这意味着你的模型或规格在给定范围内已成功通过了所有的断言检查。换句话说&#xff0c;对于你所定义…

SOLIDWORKS PDM 独立程序 C#

本主题介绍如何创建登录到 一个 SOLIDWORKS PDM Professional 文件库&#xff0c;并列出根文件夹中的文件。 启动Visual Studio.文件 > 新建 > 项目 > Visual C# > WPF&#xff08;也可以使用WF&#xff09; 输入程序名称选择存储路径确定在解决方案资源管理器中…

(学习日记)2023.06.06

写在前面&#xff1a; 由于时间的不足与学习的碎片化&#xff0c;写博客变得有些奢侈。 但是对于记录学习&#xff08;忘了以后能快速复习&#xff09;的渴望一天天变得强烈。 既然如此 不如以天为单位&#xff0c;以时间为顺序&#xff0c;仅仅将博客当做一个知识学习的目录&a…

【Linux】基础文件IO、动静态库的制作和使用

基础IO 前言回顾C语言文件IO操作三个标准 系统文件I/O系统调用接口不带mode的open带mode的openwirtereadopen的第二个参数flagsopen返回值文件的管理0&#xff0c;1&#xff0c;2演示文件描述符的分配规则重定向C中的0、1、2输入重定向追加重定向 另一种重定向的方式dup2实现输…

MySQL5.7主从同步配置(一台master,两台slave)

1. 下载MySQL(5.7.42) rpm -ivh http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm2.安装 yum install mysql-server2.1 安装过程中如果报错如下&#xff0c;按下边方法处理。否则略过即可 2.2 解决方案&#xff1a;执行以下命令 rpm --import https…

mongodb redis mysql 区别

一、MySQL 关系型数据库。 在不同的引擎上有不同 的存储方式。 查询语句是使用传统的sql语句&#xff0c;拥有较为成熟的体系&#xff0c;成熟度很高。 开源数据库的份额在不断增加&#xff0c;mysql的份额页在持续增长。 缺点就是在海量数据处理的时候效率会显著变慢。 二、Mo…

Vue3中setup函数、以及父子组件传值讲解

文章目录 1.vue3中setup函数的执行时机2.setup函数的两种写法2.1 普通写法2.2 语法糖写法 3.vue3父组件给子组件传值。4.vue3子组件给父组件传值 1.vue3中setup函数的执行时机 setup选项的写法和执行时机&#xff0c;setup函数在beforeCreate函数之前执行&#xff0c;并且是自…

以安全为底线 共迎机遇和挑战|2023 开放原子全球开源峰会可信基础设施技术分论坛即将启幕

蚂蚁集团的业务领域&#xff0c;对于「可信」有非常高的技术要求。这种可信技术不仅体现在可靠、健壮&#xff0c;也体现在金融领域独有的风控难题以及分布式系统中持续提供服务的续航能力。可信基础设施中有大量的开源项目&#xff0c;而新的机会也在不断涌现。 2023 开放原子…

echarts 图表导出PDF(带滚动条)/图片导出PDF

echarts 图表导出PDF[带滚动条]/图片导出PDF 效果展示提出问题思考问题解决问题导出PDF 里面的页头中文乱码问题参数说明 效果展示 提出问题 在开发过程中,有需求是将展示出来的echarts图表导出为pdf 原本我的滚动条是使用echarts图表进行的滚动,但通过了解后得知,echarts图表如…

《人月神话》阅读推荐

用了两周的时间&#xff0c;大致过了一遍。书中讲述的很多方面可能此时并没有很深刻的体会&#xff0c;但是该书的预见性和分析还是很让人钦佩的。书中对项目、产品、程序、程序员等一系列对象的分析是相当精准的。虽然距今已有四十多年&#xff0c;但很多依旧在发生。   书中…

Java设计模式(四)

系列文章目录 UML类图 文章目录 系列文章目录前言一、UML类图二、UML基本介绍三、UML图1.类图—依赖关系(Dependence)2.类图—泛化关系(generalization)3.类图—实现关系(Implementation)4.类图—关联关系(Association)5.类图—聚合关系(Aggregation)6.类图—组合关系(Composi…