快速入门VUE

VUE是js框架,类似我们java使用的springboot,让你开发更快速。
引入VUE,类似maven引入springboot。
他的几个常用功能

v-modelinput的内容跟vue中的变量双向绑定,input变化变量跟这变化,变量变化input也会变化
v-for:类似foreach 遍历数组的,可以用v-text填充 或者使用{{data2}}
v-modelinput的内容跟vue中的变量双向绑定,input变化变量跟这变化,变量变化input也会变化
v-bind用来修改标签的属性,比如img标签的src属性,可以做到动态修改图片
@clike,@keydown标签绑定事件

在html我们使用
引入vue

<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>

new 一个vue出来

<script>
  var app = new Vue({
    el: '#app',//作用域只在id app下,超出这个标签无效
    data: { //定义成员变量,必须先定义才能使用
      jokes: [],
      num: 1
    },
    methods: {//定义方法
      get: function () {
       jokes=["洒家看到了"];
        }
  })
</script>

使用他

<div id="app" >
  <button @click="get()" >get方法</button>
  <div>
    <ul>
      <li   v-for="(note,index) in jokes"><!--类似foreach,遍历我们定义的那个成员变量,当joke变化时,这个也会重新遍历一次-->
        <span style="color: red">
          {{ index+1 }} -{{ note }}
        </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      </li>
    </ul>
    <span >
      总个数:{{jokes.length}}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    </span>
  </div>
</div>

完整实例代码

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>vue</title>
</head>
<!--  引入VUE-->
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<!--  引入axios-->
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>

<body>
<div id="app" style="margin-top: 200px;margin-left: 30%">

  <h2> <input type="text"  v-model="num" @keydown.enter="get()">输入要查询的条数 ,按回车查询  </h2>
  <button @click="get()" >get方法</button>



  <button @click="post()" >post方法</button>

  <div>
    <ul>
      <li   v-for="(note,index) in jokes">
        <span style="color: red">
          {{ index+1 }} -{{ note }}
        </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      </li>
    </ul>
    <span >
      总个数:{{jokes.length}}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    </span>

  </div>
</div>


<script>


  var app = new Vue({
    el: '#app',//作用域只在id app下,超出这个标签无效
    data: {
      jokes: [],
      num: 1
    },
    methods: {
      get: function () {
        <!--在axios this不在是外部的this了 所以要现在外面赋值-->
        var that=this;

       axios.get("https://autumnfish.cn/api/joke/list?num="+that.num)
        .then(function (res){
          console.log(res);
          that.jokes=res.data.jokes;
        },function (err){
          alert(err);
        });
      },
      post: function () {
        var that=this;

        axios.post(
          "https://autumnfish.cn/api/user/reg",
          {username:"jack"}
        )
          .then(function (res){
            console.log(res);
            that.jokes=res.data;
          },function (err){
            alert(err);
          });
      }
    }
  })
</script>
</body>

</html>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值