Vue 条件渲染 双向绑定

https://www.dedao.cn/ebook/reader?id=5lZOKpMGr9mgdOvYa6Ej75XRo1NML3jx810k8ZVzb2nqPpDxBeJlK4AyQ8RPQv2z

v-if实现条件渲染的功能。v-model实现双向数据传输。

v-model用来进行双向绑定,当输入框中的文字变化时,其会将变化同步到绑定的变量上,同样,当我们对变量的值进行改变时,输入框中的文本也会对应变化。

看起来像是登录界面,实际上功能缺失比较多。主要是没有数据的比较。

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>loginDemo</title>
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
</head>

<body>
    <div id="Application" style="text-align: center;">
        <h1>{
  {title}}</h1>
        <div v-if="noLogin">Account:
            <input v-model="userName" type="text">
        </div>
        <div v-if="noLogin">Password:
            <input v-model="password" type="password">
        </div>
        <div v-on:click="click" style="border-radius: 30px;
                width: 100px;
                margin: 20px auto;
                color: black;
                background-color: grey;
                ">{
  {buttonTitle}} </div>
    </div>
    <script>
        const App = {
            data() {
                return {
                    title: "欢迎:未登录",
                    noLogin: true,
                    userName: "",
                    password: "",
                    buttonTitle: "登录"
                }
            },
            methods: {
                click() {
                    if (this.noLogin) {
                        this.login()
                    } else {
                        this.logout()
                    }
                },
                login() {
                    if (this.userName.length > 0 && this.password.length > 0) {
                        alert(`userName:${this.userName}`);
                        this.noLogin = false;
                        this.title = `欢迎: ${this.userName}`;
                        this.buttonTitle = "退出";
                        this.userName = "";
                        this.password = "";
                    } else {
                        alert("请输入账号和密码");
                    }
                },
                logout() {
                    this.noLogin = true;
                    this.title = `欢迎: 未登录`;
                    this.buttonTitle = "登录";
                }
            }
        }
        Vue.createApp(App).mount("#Application")
    </script>

</body>

</html>

运行结果:

相关推荐

  1. vue 双向

    2024-02-07 04:04:03       36 阅读
  2. Vue 双向数据

    2024-02-07 04:04:03       40 阅读
  3. vue双向/小程序双向

    2024-02-07 04:04:03       44 阅读
  4. Vue双向数据原理

    2024-02-07 04:04:03       63 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-07 04:04:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-07 04:04:03       101 阅读
  3. 在Django里面运行非项目文件

    2024-02-07 04:04:03       82 阅读
  4. Python语言-面向对象

    2024-02-07 04:04:03       91 阅读

热门阅读

  1. 踩坑实录(First Day)

    2024-02-07 04:04:03       47 阅读
  2. 2024/2/5

    2024-02-07 04:04:03       45 阅读
  3. Linux内核与驱动面试经典“小”问题集锦(3)

    2024-02-07 04:04:03       51 阅读
  4. 大模型: 流式会话的实现方式

    2024-02-07 04:04:03       56 阅读
  5. 【PHP】TP5.0模型关联搜索查询

    2024-02-07 04:04:03       46 阅读
  6. C++ access 的作用

    2024-02-07 04:04:03       49 阅读
  7. 综合分享2

    2024-02-07 04:04:03       48 阅读
  8. 【量子通信】量子通信技术:前景与挑战

    2024-02-07 04:04:03       50 阅读
  9. docker 部署springboot项目详细步骤

    2024-02-07 04:04:03       50 阅读