• Vue-03基础


    本节:依旧是明歌的视频。

    这部分主要是组合式API vue3的写法。

    一、自定义组件emit事件(子传父)  this.$emit("自定义方法名");

    直接在方法写表达式,比较快,直接把变量设置成false 

     二、slot插槽(父传子)

     这样写只能实现内容显示,如果子组件需要存储变量,进行更深的逻辑,则需要prop来接受父页面传过来的变量。 

     子组件也可以在插槽传值给父组件。

     效果:

    三、使用v-model,子组件改变父组件的内容。

     

     PS:键盘监听,如果按回车,子件就修改内容,按esc就消失子件。

     我想要按下回车之后,不只是修改,还同时让子件消失。我写了个变量放在修改的方法里面,一开始为真,然后写监听事件,那个变量一发生变化,就调用让组件消失的方法。

     四、组合式API,也就是vue3

    我们通过组合式 API 解决了两个问题。

    1.我们让组件拥有了更加良好的代码组织结构
    2.我们让相同的代码逻辑在不同的组件中进行了完整的复用

    (不用写this了)

    (1)ref修改变量,本质和获取dom,修改id差不多,只是写法不同 。变量是 变量名.value ,数组是通过下标来取值。

     因为组合式API写法不一样,所以(2)生命周期的写法也不一样。

    1.首先引入   2.写法的不同

     (3)watch和computed的写法

     1.首先引入   2.写法的不同

    (4)props和emit还有方法的写法。

     (5)子组件的引入

    ps:新建完还遇到一个问题就是,怎么调样式都不正确,然后到处注释,其实是最简单的,就是我找到了整个项目唯一的style代码,全部注释了就可以了

    以上所有内容的代码

    1.父页面

    1. <template>
    2. <div>
    3. <div @click="clickMe1">{{ text1.name }}div>
    4. <div>
    5. {{ "今天天气是:" + weather + "现在时间是" + new Date().toISOString() }}
    6. div>
    7. <hr>
    8. <div>{{ todata }}div>
    9. <button @click="showme = !showme"> 点击出现招呼button>
    10. <son-one v-model="weather" content="你是山间的清泉,你是夏天傍晚的微风" @closeOne="showme = false" v-if="showme">
    11. <template #content="contentindex">
    12. <div>{{ contentindex.meimei }}div>
    13. <div>第一百次心动是:{{ weather }}div>
    14. template>
    15. son-one>
    16. div>
    17. template>
    18. <script>
    19. import sonOne from './components/son.vue'
    20. export default {
    21. components: { sonOne },
    22. data() {
    23. return {
    24. weather: "雨天",
    25. showme: false,
    26. text1: {
    27. name: "张楚梅"
    28. }
    29. };
    30. },
    31. methods: {
    32. clickMe1() {
    33. this.text1.name = "袁楚天,天天"
    34. },
    35. },
    36. computed: {
    37. todata() {
    38. return "今天天气是:" + this.weather + ",现在时间是" + new Date().toISOString()
    39. },
    40. },
    41. watch: {
    42. text1: {
    43. handler: function (newVal, oldVal) {
    44. console.log(newVal, oldVal);
    45. },
    46. deep: true
    47. },
    48. weather: {
    49. handler: function (newVal, oldVal) {
    50. console.log(newVal, oldVal);
    51. },
    52. deep: true
    53. }
    54. }
    55. }
    56. script>
    57. <style scoped>
    58. style>

    子组件:

    1. <template>
    2. <div style="background-color: pink;width: 400px;height: 400px;">
    3. <div>早上好,公主</div>
    4. <div>
    5. {{ content }}
    6. <slot meimei="梅梅" name="content"></slot>
    7. </div>
    8. <div><input v-model="res" @keyup.enter="hello" @keyup.esc="cancel"></div>
    9. <button @click="hello">你好哇</button>
    10. <button @click="cancel">再见</button>
    11. </div>
    12. </template>
    13. <script>
    14. export default {
    15. data() {
    16. return {
    17. res: "",
    18. watch1: false,
    19. }
    20. },
    21. watch: {
    22. watch1:
    23. {
    24. handler: function (newVal, oldVal) {
    25. console.log(newVal, oldVal);
    26. this.cancel();
    27. },
    28. deep: true
    29. }
    30. },
    31. methods: {
    32. hello() {
    33. this.$emit("update:model-value", this.res);
    34. this.$emit("close", { name: "小天" });
    35. this.watch1 = true;
    36. },
    37. cancel() {
    38. console.log(this.content)
    39. this.$emit("closeOne");
    40. },
    41. },
    42. props: {
    43. content: {
    44. type: String,
    45. default: "",
    46. },
    47. },
    48. modelValue: {
    49. type: String,
    50. default: "",
    51. },
    52. }
    53. </script>

  • 相关阅读:
    黑豹程序员-架构师学习路线图-百科:MVC的演变终点SpringMVC
    【C#学习】button:只显示图片
    数据结构笔记(王道考研) 第四章:串
    xx科技2023前端开发卷B
    猿桌派第三季开播在即,打开出海浪潮下的开发者新视野
    mysql 8.0.28 查询语句执行顺序实测结果
    猿创征文|从JAVAER到GISER的进击之路
    Redis 6.0源码学习 String类型
    SpringSecurity - 启动流程分析(五)
    ts 联合react 实现ajax的封装,refreshtoken的功能
  • 原文地址:https://blog.csdn.net/m0_59214979/article/details/126401599