vue.js入门demo

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Vue的第一个入门案例</title>
</head>
<body>
<div id="app"></span><!-- 作用范围以内都能使用  {{变量名}}-->
    <p>{{msg}}</p>
    <p>用户名称{{user.username}}</p>
    <p>用户密码{{user.pwd}}</p>
    <p>用户名称{{users[0].username}}</p>
    <p>用户密码{{users[0].pwd}}</p>
    <p>{{min.toUpperCase()}}</p>
    <p>{{min+":我是小写"}}</p>
</div>
<!-- 开发环境版本,包含了有帮助的命令行警告 -->
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
    const app = new Vue({
        el:"#app",<!-- el 里必须带双引号 id #app  class .app  与div中对映-->
        data:{
            msg:"你好",
            user:{"username":"张三","pwd":123456},
            users:[{"username":"张三","pwd":123456},{"username":"李四","pwd":123456}],
            min:"abcd",
        }
    });
</script>
</body>
</html>

1.vue实例(对象)中el属性:代表Vue的作用范围,日后Vue的作用范围内都可以使用Vue的语法

2.vue实例(对象)中data属性:用来给Vue实例绑定一些相关数据,绑定的数据可以通过{{变量名}}在Vue作用范围内取出

3.在使用{{}}进行获取data中数据时,可以在{{}}中书写表达式,运算符,调用相关方法,以及逻辑运算等

4.el属相中可以书写任意的CSS选择器[jquery选择器],但是在使用Vue开发是推荐使用  id选择器

v-text与v-thml的区别

v-text会将数据原样渲染到标签中,会覆盖原标签中的数据,可以避免插值闪烁

v-thml会将HTML中的标签解析后渲染到标签中,会覆盖原标签中的数据,可以避免插值闪烁

绑定事件v-on:click 与@:click 

<!DOCTYPE html>
<html lang="en" xmlns:v-no="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Vue的第一个入门案例</title>
</head>
<body>
<div id="app"></span>
    <input type="button" value="+" @click="addCount">
<!--    <input type="button" value="+" v-no:click="addCount">  与上面一样作用-->
    <p v-text="count_1"></p>
    <input type="button" value="-" @click="delCount">
</div>
<!-- 开发环境版本,包含了有帮助的命令行警告 -->
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
    const app = new Vue({
        el:"#app",
        data:{
            count_1:1,
        },methods:{
            // delCount:function(){
            //  与下面一样
            // },
            delCount(){
                if(this.count_1<2){
                    alert("不能再减了");
                    return;
                }
                --this.count_1;
            },addCount(){a
                if(this.count_1>9){
                    alert("不能再加了");
                    return;
                }
                ++this.count_1;
            }
        },
    });
</script>
</body>
</html>

v-bind与:相同 v-if v-show 

<!DOCTYPE html>
<html lang="en" xmlns:v-no="http://www.w3.org/1999/xhtml" xmlns:v-bind="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Vue的第一个入门案例</title>
    <style>
        .red{
            border: red solid 2px;
        }
    </style>
</head>
<body>
<div id="app"></span>
<!--    <input type="button" v-bind:class="{red:showClass}" value="变颜色" @click="showClass_1">-->
    <input type="button" :class="{red:showClass}" value="变颜色" @click="showClass_1">
    <input type="button" v-show="showClass" value="太阳">
    <input type="button" v-if="!showClass" value="黑">
    <input type="button" v-if="showClass" value="白">

</div>
<!-- 开发环境版本,包含了有帮助的命令行警告 -->
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
    const app = new Vue({
        el:"#app",
        data:{
            showClass:false,
        },methods:{
            showClass_1(){
                this.showClass = !this.showClass;
            },
        },
    });
</script>
</body>
</html>

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值