当前位置: 首页 > news >正文

vue基础

引入vue文件

<div id="app"><!--{{}}插值表达式,绑定vue中的data数据-->{{message}}
</div><script src="vue.min.js"></script>
<script>new Vue({el:'#app',data:{message:'Hello Vue'}})
</script>

单项和双向绑定指令

<div id="app"><div v-bind:style="msg">单向绑定</div><!--简写--><div :style="msg">单向绑定</div>
</div>
<script src="vue.min.js"></script>
<script>new Vue({el:'#app',data:{msg:'color:red;'}})
</script>
<div id="app">{{keyword}}<input type="text" :value="keyword"/><!--双向绑定--><input type="text" v-model="keyword"/> 
</div>
<script src="vue.min.js"></script>
<script>new Vue({el:'#app',data:{keyword:'jordan'}})
</script>

绑定事件

v-on:事件名称=“调用方法” 简写 @事件名称

<div id="app"><button v-on:click="show()">事件绑定</button><button @click="show()">事件绑定2</button>
</div>
<script src="vue.min.js"></script>
<script>new Vue({el:'#app',data:{msg:'color:red;'},methods:{show(){console.log("show……")}}})
</script>

条件指令

v-if:条件判断
v-else

<div id="app"><input type="checkbox" v-model="ok"/><div v-if="ok">选中了</div><div v-else>没有选中</div>
</div>
<script src="vue.min.js"></script>
<script>new Vue({el:'#app',data:{ok:false}})
</script>

循环指令

v-for

<div id="app"><div v-for="user in userList">{{user.name}}---{{user.age}}</div><!--带上索引遍历--><div v-for="(user,index) in userList">{{index}}--{{user.name}}---{{user.age}}</div>
</div>
<script src="vue.min.js"></script>
<script>new Vue({el:'#app',data:{userList:[{"name":"jordan","age":23},{"name":"james","age":30}]}})
</script>

vue的页面渲染之前执行的created方法和页面渲染之后执行的mounted方法

<div id="app">{{msg}}
</div>
<script src="vue.min.js"></script>
<script>new Vue({el:'#app',data:{msg:'hello'},created(){debuggerconsole.log('created.....')},mounted(){debuggerconsole.log('mounted')}})
</script>

axios使用

独立于vue的一个项目,可以用于浏览器和node.js中发送ajax请求

准备一个模拟后端返回的json数据

{"code":200,"message":"成功","data":{"items":[{"name":"jordan","age":20},{"name":"kobe","age":19},{"name":"james","age":18}]}
}
<div id="app"><!--页面显示数据--><table><tr v-for="user in userList"><td>{{user.name}}</td><td>{{user.age}}</td></tr></table>
</div><script src="vue.min.js"></script>
<script src="axios.min.js"></script>
<script>new Vue({el:"#app",data:{userList:[]},created(){//页面渲染之前执行,调用方法,返回json数据this.getList()},methods:{getList(){//使用axios方式ajax请求路径axios.get("user.json").then(response =>{console.log(response)this.userList = response.data.data.itemsconsole.log(this.userList)}) //请求成功.catch(error =>{console.log(error)}) //请求失败}}})
</script>
http://www.lryc.cn/news/193229.html

相关文章:

  • 访问量突破1W,纪念一下~
  • C# 处理TCP数据的类(服务端)
  • 【Jenkins】调用API构建并钉钉通知
  • Java NIO三大核心组件
  • js数据排序方法(sort)?
  • 若依框架学习笔记_mybatis
  • 虚拟机的发展史:从分时系统到容器化
  • 季涨约3~8%,DRAM合约价大幅回升 | 百能云芯
  • LocalDate的用法
  • React通过ref获取子组件的数据和方法
  • Enhancing Self-Consistency and Performance of Pre-Trained Language Model
  • 安防监控视频汇聚平台EasyCVR视频广场搜索异常,报错“通道未开启”的问题排查与解决
  • css 星星闪烁加载框
  • 代码随想录算法训练营第二十二天丨 二叉树part09
  • Apipost连接数据库详解
  • 让 Visual Studio 用上 ChatGPT
  • 如何删除错误堆栈里的数据
  • k8s使用minio分布式集群作为存储--基础配置篇
  • @Autowired 到底是怎么把变量注入进来的?
  • 【Python学习笔记】函数
  • 简单实现一个todoList(上移、下移、置顶、置底)
  • 计算机视觉:池化层的作用是什么?
  • luffy项目前端创建、配置、解决跨域问题、后端数据库迁移
  • 电商数据API接口:新服务下电商网站、跨境电商独立站,移动APP的新型拉新武器
  • 多线程并发篇---第十一篇
  • JVM第六讲:JVM 基础 - Java 内存模型引入
  • 机房安全管理制度
  • 【自然语言处理】— 隐马尔可夫模型详解、例解
  • 运行的 akrun 会打印信息到控制台,如何取消打印 -- chatGPT
  • 【React】03-React面向组件编程2