一、模板语法

  1. <template>
  2. // 在Vue2中template标签中只能有一个根元素,在Vue3中没有这样限制
  3. </template>
  4. <script setup> // 这里的setup是加在了script标签里
  5. </script>
  6. <style scoped>
  7. // 支持CSS变量使用 v-bind(变量)
  8. </style>

二、data数据定义以及函数的声明

  1. <template>
  2. <div class="home">
  3. <div @click="msgClick">
  4. {{msg}}
  5. </div>
  6. <div @click="stateMsgClick">
  7. {{state.reactive}}
  8. </div>
  9. </div>
  10. </template>
  11. <script setup>
  12. import { reactive, ref } from "vue";
  13. const msg = ref("声明的基本msg"); //ref声明的基本类型响应式数据
  14. //reactive 声明复杂类型响应式数据
  15. const state =reactive({
  16. stateMsg:"state的msg",
  17. })
  18. //箭头函数的声明
  19. const msgClick = ()=>{
  20. console.log(msg.value) // 结果:声明的基本msg
  21. /*
  22. 如果修改ref声明的数据时 可以用数据.value来进行赋值操作修改
  23. 例如: msg.value = "想要修改成的值" 即可
  24. */
  25. }
  26. // function 关键字声明的函数
  27. function stateMsgClick (){
  28. console.log(state.stateMsg) // 结果: state的msg
  29. /*
  30. 如果修改reactive声明的数据时 可以直接进行赋值操作修改
  31. 例如: state.stateMsg = "想要修改成的值" 即可
  32. */
  33. }
  34. </script>

三、computed、watch、watchEffect

computed:

  1. <script setup>
  2. import { reactive,computed} from "vue";
  3. const state = reactive({
  4. count: 1
  5. })
  6. const computedCount = computed(()=>state.count)
  7. //会自动计算state的count值 切记computed的值不能更改
  8. watchEffect(() => console.log(state.count))
  9. setTimeout(() => {
  10. state.count++
  11. }, 1000)
  12. </script>

watchEffect:

  1. <script setup>
  2. import { reactive,watchEffect } from "vue";
  3. const state = reactive({
  4. count: 1
  5. })
  6. watchEffect(() => console.log(state.count))
  7. setTimeout(() => {
  8. state.count++
  9. }, 1000)
  10. </script>

Watch:

  1. <script setup>
  2. import { reactive,watch } from "vue";
  3. const state = reactive({
  4. count: 1
  5. })
  6. // 监听count
  7. watch(
  8. () => state.count,
  9. (newVal, oldVal) => {
  10. console.log(state.count)
  11. console.log(`watch监听变化前的数据:${oldVal}`)
  12. console.log(`watch监听变化后的数据:${newVal}`)
  13. },
  14. {
  15. immediate: true, // 立即执行
  16. deep: true // 深度监听
  17. }
  18. )
  19. setTimeout(() => {
  20. state.count++
  21. }, 1000)
  22. </script>

四、组件注册、父传子、子传父

组件注册:

  1. <template>
  2. <div class="parent">
  3. <child></child>
  4. </div>
  5. </template>
  6. <script setup>
  7. // 这是引入的组件 路径是我本地的路径
  8. // 直接在template模板中使用即可
  9. import child from "@/components/child";
  10. </script>

父传子:

  1. <!-- 父组件 -->
  2. <template>
  3. <div class="parent">
  4. <child :msg="msg"></child>
  5. </div>
  6. </template>
  7. <script setup>
  8. import { ref } from "vue"
  9. // 这是引入的组件 路径是我本地的路径
  10. // 直接在template模板中使用即可
  11. import child from "@/components/child";
  12. const msg=ref("这是父的msg")
  13. </script>
  14. <!-- 子组件 -->
  15. <template>
  16. <div class="child">
  17. {{msg}}
  18. </div>
  19. </template>
  20. <script setup>
  21. import { defineProps } from "vue"
  22. // 这样就可以直接在template中使用
  23. const props = defineProps({
  24. msg: String,
  25. });
  26. </script>

子传父:

  1. <!-- 子组件 -->
  2. <template>
  3. <div class="child" @click="childCk">
  4. 点击进行父传子
  5. </div>
  6. </template>
  7. <script setup>
  8. import { defineEmits,defineExpose } from "vue"
  9. // 可以声明父组件使用子组件时挂载的函数
  10. const emits = defineEmits(["childEmitFn"]);
  11. const childExport = ref("这是当前抛出去的数据")
  12. const childMsg = ref("组件通过emit调用函数来进行子传父 ")
  13. const childCk = () =>{
  14. emits("childEmitFn",childMsg.value);
  15. }
  16. // 这是导出当前组件定义的数据或者是函数方法等等内容 可以通过ref来获取
  17. defineExpose({
  18. // childExport:childExport 下面是简写
  19. childExport
  20. })
  21. </script>
  22. <!-- 父组件 -->
  23. <template>
  24. <div class="parent">
  25. <child @childEmitFn="childEmitFn"></child>
  26. </div>
  27. </template>
  28. <script setup>
  29. import { ref } from "vue"
  30. // 这是引入的组件 路径是我本地的路径
  31. // 直接在template模板中使用即可
  32. import child from "@/components/child";
  33. const childEmitFn = (value)=>{
  34. console.log(value); //结果: 子组件通过emit调用函数来进行子传父
  35. }
  36. </script>

五、v-model

  1. <!-- 父组件 -->
  2. <template>
  3. <div class="parent">
  4. <child v-model:msg="msg" v-model="modelVal"></child>
  5. </div>
  6. </template>
  7. <script setup>
  8. import { ref } from "vue"
  9. // 这是引入的组件 路径是我本地的路径
  10. // 直接在template模板中使用即可
  11. import child from "@/components/child";
  12. const msg= ref("这是父的msg")
  13. const modelVal =ref("普通的v-model")
  14. </script>
  15. <!-- 子组件 -->
  16. <template>
  17. <div class="child">
  18. {{msg}}
  19. </div>
  20. <button @click="changeParentMsg">改变父的msg</button>
  21. </template>
  22. <script setup>
  23. import { defineProps,defineEmits } from "vue"
  24. // 这样就可以直接在template中使用
  25. const props = defineProps({
  26. msg: String,
  27. modelValue: String, //如果是直接在组件上使用v-model 这个key就是对应的props键值
  28. });
  29. const emits = defineEmits(['update:msg','update:modelValue'])
  30. const changeParentMsg = () =>{
  31. emits('update:msg','子组件把父组件msg更改了')
  32. }
  33. </script>

六、slots、attrs (不常用)

  1. <!-- 父组件 -->
  2. <template>
  3. <div class="home">
  4. <child
  5. :msg="msg"
  6. :status="status"
  7. >
  8. <template #title="props">
  9. <div>{{ props }}标题</div>
  10. </template>
  11. </child>
  12. </div>
  13. </template>
  14. <script setup>
  15. import { ref, toRefs } from "vue";
  16. import child from "@/components/child";
  17. const msg = ref("父的值");
  18. const status = ref(200);
  19. </script>
  20. <!-- 子组件 -->
  21. <template>
  22. <div class="" >
  23. {{ msg }}
  24. <slot> </slot>
  25. <slot name="title" title="这是子传过来的东西"></slot>
  26. </div>
  27. </template>
  28. <script setup>
  29. import {
  30. defineProps,
  31. ref,
  32. useSlots,
  33. useAttrs,
  34. } from "vue";
  35. // props 现在props里就声明了msg这个参数 可以看到父组件还传了一个status属性这里没有声明
  36. const props = defineProps({
  37. msg: String,
  38. });
  39. const attrs = useAttrs(); // 那么就可以用attrs来接收父组件传的属性未在props中声明的
  40. console.log(attrs);
  41. const slots = useSlots(); // 这个是可以查看该组件被调用时用了哪些插槽
  42. console.log(slots);
  43. console.log(slots.title());
  44. </script>

七、style标签class中使用变量

  1. <template>
  2. <span>class使用变量</span>
  3. </template>
  4. <script setup>
  5. import { reactive } from 'vue'
  6. const styleState = reactive({
  7. color: 'blue'
  8. })
  9. </script>
  10. <style scoped>
  11. span {
  12. // v-bind使用styleState中的变量
  13. color: v-bind('styleState.color');
  14. }
  15. </style>

八、对await的支持 :

  1. <script setup>
  2. //无需再使用async 可以直接用 await 因为用了这个之后 组件的setup就会自动变成 async setup
  3. const GET = await axios('/api').then(() => {})
  4. </script>