开发日记13-响应式变量

先展示home.vue

复制代码
<template>
  <a-layout>
    <the-header></the-header>
    <a-layout-content style="padding: 0 50px">
      <a-breadcrumb style="margin: 16px 0">
        <a-breadcrumb-item>Home</a-breadcrumb-item>
        <a-breadcrumb-item>List</a-breadcrumb-item>
        <a-breadcrumb-item>App</a-breadcrumb-item>
      </a-breadcrumb>
      <a-layout style="padding: 24px 0; background: #fff">
        <the-sider></the-sider>
        <a-layout-content :style="{ padding: '0 24px', minHeight: '280px' }">
          {{ resp || "loading... "}}
          <a-input v-model:value="resp" @change="onchange()"></a-input>
        </a-layout-content>
      </a-layout>
    </a-layout-content>
    <a-layout-footer style="text-align: center">
      Ant Design ©2018 Created by Ant UED
    </a-layout-footer>
  </a-layout>
</template>

<script setup>
import TheHeader from "../components/the-header.vue"
import TheSider from "../components/the-sider.vue"
import axios from "axios"
import {ref} from "vue";
import {message} from "ant-design-vue"

const resp = ref()

const onchange = () => {
  console.log("手工赋值,内容:", resp.value)
}

axios.get("http://localhost:18000/nls/query", {
  params: {
    "mobile": "Huawei"
  }
}).then((response)=>{
  console.log(response)
  let data = response.data
  if (data.success) {
    resp.value = data.content
    console.log("已赋值,内容:", resp.value)
  } else {
    message.error(data.message)
  }
})
</script>

<style scoped>
.ant-row-rtl .logo {
  float: right;
  margin: 16px 0 16px 24px;
}
</style>

在里面我们引入了一个响应式变量resp,用ref去定义,ref是响应式变量的关键字

那么这个变量就涉及到读和写以及展示

在脚本中,我们利用axios获取后端传来的结果

javascript 复制代码
axios.get("http://localhost:18000/nls/query", {
  params: {
    "mobile": "Huawei"
  }
}).then((response)=>{
  console.log(response)
  let data = response.data
  if (data.success) {
    resp.value = data.content
    console.log("已赋值,内容:", resp.value)
  } else {
    message.error(data.message)
  }
})

如果成功获取结果,那么我们就对这个resp赋值,值得注意的是,我们赋值的话,需要赋值给resp的value属性,这个一定要记住

javascript 复制代码
resp.value = data.content

那么读取之后,如何展示呢? 在template中使用两个花括号{{ resp }}

javascript 复制代码
        <a-layout-content :style="{ padding: '0 24px', minHeight: '280px' }">
          {{ resp }}
        </a-layout-content>

这样的话,成功取到后端结果后,会立即将对象以字符串形式展示到html中,这就叫响应式。

另外如何修改或者写入呢?

我们引入一个输入的标签a-input,绑定对应的变量resp,并且绑定一个onchange回调函数,一旦内容改变,就会在控制台打印内容,当我们在输入框输入数字时,响应式变量自身发生改变,同时也看到控制台的打印123...

javascript 复制代码
<a-input v-model:value="resp" @change="onchange()"></a-input>
相关推荐
我不叫武1 小时前
一个用 Rust 写的离线编码查询桌面工具
前端
爱写代码的小朋友2 小时前
从零开始学 Win32 API:C++ 窗口编程实战(VS Code + MinGW-w64 命令行详解)
开发语言·c++
果汁华2 小时前
Function Calling 与 Python 实战完整指南
开发语言·网络·python
Web4Browser2 小时前
指纹浏览器 API 自动化怎么接:启动 Profile、获取 CDP 端点并连接自动化框架
前端·网络·typescript·自动化
Patrick_Wilson2 小时前
从 React 到 Flutter:写给前端的一张跨端知识地图
前端·flutter·react.js
颜酱2 小时前
06 | 把 meta_config 同步进 MySQL(生成阶段)
前端·人工智能·后端
小小晓.2 小时前
C++:语句和作用域
开发语言·c++
OpenTiny社区3 小时前
Loop Engineering:让 AI Agent 自己跑起来的工程方法
前端·github
wanderist.3 小时前
Lambda表达式在算法竞赛中的应用
java·开发语言·算法
IT_陈寒3 小时前
SpringBoot自动配置坑了我一周,原来问题这么蠢!
前端·人工智能·后端