• vue ref和$refs获取组件实例


    vue ref和$refs获取组件实例

    在这里插入图片描述
    **创建 工程:
    H:\java_work\java_springboot\vue_study

    ctrl按住不放 右键 悬着 powershell

    H:\java_work\java_springboot\js_study\Vue2_3入门到实战-配套资料\01-随堂代码素材\day04\准备代码\15-ref和$refs获取组件实例

    vue --version
    vue create v-ref-zj-demo
    cd v-ref-zj-demo

    npm run serve

    App.vue

    <template>
      <div class="app">
        <h4>父组件 -- <button>获取组件实例</button></h4>
        <BaseForm ref="baseForm"></BaseForm>
        <button @click="handleGet">获取数据</button>
        <button @click="handleReset">重置数据</button>
      </div>
    </template>
    
    <script>
    import BaseForm from "./components/BaseForm.vue";
    export default {
      components: {
        BaseForm,
      },
      methods: {
        handleGet() {
          console.log(this.$refs.baseForm.getFormData());
        },
        handleReset() {},
      },
    };
    </script>
    
    <style>
    </style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    BaseForm.vue

    <template>
      <div class="app">
        <div>账号: <input v-model="username" type="text" /></div>
        <div>密码: <input v-model="password" type="text" /></div>
        <div>
          <button @click="getFormData">获取数据</button>
          <button @click="resetFormData">重置数据</button>
        </div>
      </div>
    </template>
    
    <script>
    export default {
      data() {
        return {
          username: "admin",
          password: "123456",
        };
      },
      methods: {
        getFormData() {
          console.log("获取表单数据", this.username, this.password);
          return {
            username: "admin",
            password: "123456",
          };
        },
        resetFormData() {
          this.username = "";
          this.password = "";
          console.log("重置表单数据成功");
        },
      },
    };
    </script>
    
    <style scoped>
    .app {
      border: 2px solid #ccc;
      padding: 10px;
    }
    .app div {
      margin: 10px 0;
    }
    .app div button {
      margin-right: 8px;
    }
    </style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    在这里插入图片描述

  • 相关阅读:
    馒头的1day漏洞巡舰系统
    Vue.js+Node.js全栈开发教程:Vue.js指令详解
    【Python数据科学快速入门系列 | 06】Matplotlib数据可视化基础入门(一)
    Vue3.X笔记总结
    JAVA加密算法
    给你的R语言再次提速
    【软件工程与实践】(第四版)第6章习题答案详解
    技术人员转团队管理该怎么做,这篇文章告诉你。
    视锥体裁剪射线的算法
    后端每日一题 1:说一下三次握手
  • 原文地址:https://blog.csdn.net/wowocpp/article/details/133886483