ref和$refs Vue异步更新 $nextTick

作用:利用ref和$refs可以用于获取dom元素或组件实例

特点:在当前组件内查找(更精确更稳定)

1.获取dom元素:

首先给目标元素添加ref属性

然后通过this.$refs.xxx获取目标标签

<template>
  <div ref="mychart" class="base-chart-box"></div>
</template>

<script>
import * as echarts from "echarts";

export default {
  mounted() {
    // 基于准备好的dom,初始化echarts实例
    const myChart = echarts.init(this.$refs.mychart);
    // 绘制图表
    myChart.setOption({
      title: {
        text: "ECharts 入门示例",
      },
      tooltip: {},
      xAxis: {
        data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"],
      },
      yAxis: {},
      series: [
        {
          name: "销量",
          type: "bar",
          data: [5, 20, 36, 10, 10, 20],
        },
      ],
    });
  },
};
</script>

<style scoped>
.base-chart-box {
  width: 400px;
  height: 300px;
  border: 3px solid #000;
  border-radius: 6px;
}
</style>

2.获取组件:

首先给目标组件添加ref属性

然后通过this.$refs.xxx获取目标组件

子组件:

<template>
  <div class="app">
    <div>账号: <input v-model="account" type="text" /></div>
    <div>密码: <input v-model="password" type="text" /></div>
    <div></div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      account: "admin",
      password: "123456",
    };
  },
  methods: {
    //方法一:收集表单数据,返回一个对象
    getValues() {
      return {
        account: this.account,
        password: this.password,
      };
    },
    //方法二:重置表单
    resetValues() {
      this.account = "";
      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>

父组件:

<template>
  <div class="app">
    <BaseForm ref="baseForm"></BaseForm>
    <button @click="handleGet">获取数据</button>
    <button @click="handlereset">重置数据</button>
  </div>
</template>

<script>
import BaseForm from "../vue-project/src/components/BaseForm.vue";
export default {
  components: {
    BaseForm,
  },
  methods: {
    handleGet() {
      //可以调用子组件的方法
      console.log(this.$refs.baseForm.getValues());
    },
    handlereset() {
      this.$refs.baseForm.resetValues();
    },
  },
};
</script>

<style>
</style>

vue是异步更新DOM(提升性能)

$nextTick等DOM更新后,才会触发执行此方法里的函数体

<template>
  <div class="app">
    <div v-if="isShowEdit">
      <input ref="inp" type="text" v-model="editValue" />
      <button>确认</button>
    </div>
    <div v-else>
      <span>{{ title }}</span>
      <button @click="handleEdit">编辑</button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      title: "大标题",
      isShowEdit: false,
      editValue: "",
    };
  },
  methods: {
    handleEdit() {
      //1.显示输入框(异步dom更新)
      this.isShowEdit = true;
      //2.让输入框获取焦点
      this.$nextTick(() => {
        this.$refs.inp.focus();
      });
    },
  },
};
</script>

<style>
</style>

  • 9
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
1. $refs: $refs 是 Vue 实例提供的一个属性,用于访问组件或元素的子组件或子元素。可以通过在元素上定义 ref 属性来注册引用,然后在 Vue 实例中通过 $refs 访问到该元素或组件。 示例: ```html <template> <div> <child-component ref="child"></child-component> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, mounted() { // 通过 $refs 访问 ChildComponent 实例 console.log(this.$refs.child); } } </script> ``` 2. $slot: $slot 是 Vue 实例提供的一个属性,用于访问组件的插槽内容。可以通过在组件模板中使用 <slot> 标签来定义插槽,然后在父组件中通过 $slot 访问到插槽内容。 示例: ```html <!-- ChildComponent.vue --> <template> <div> <slot></slot> </div> </template> <!-- ParentComponent.vue --> <template> <div> <child-component> <p>Hello, world!</p> </child-component> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, mounted() { // 通过 $slot 访问 ChildComponent 的插槽内容 console.log(this.$slot.default); } } </script> ``` 3. $nextTick: $nextTickVue 实例提供的一个方法,用于在 DOM 更新之后执行回调函数。由于 Vue 异步执行 DOM 更新,所以在修改数据之后立即获取新的 DOM 元素可能会获取到旧的值,需要通过 $nextTick 等待 DOM 更新完成后再获取新的值。 示例: ```html <template> <div> <p ref="message">{{ message }}</p> <button @click="changeMessage">Change message</button> </div> </template> <script> export default { data() { return { message: 'Hello, world!' } }, methods: { changeMessage() { this.message = 'New message'; this.$nextTick(() => { // 在 DOM 更新之后获取新的值 console.log(this.$refs.message.textContent); // New message }); } } } </script> ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值