大灰狼网站更新升级通知,做系统哪个网站上的好,中国老区建设促进会网站,学平面设计网站前端:Vue.js学习 1. 第一个Vue程序2. Vue指令2.1 v-if、v-else-if、v-else2.2 v-for2.3 事件绑定 v-on:2.4 v-model 数据双向绑定2.5 v-bind 绑定属性 3. Vue组件4. Vue axios异步通信5. 计算属性6. 插槽 slots7. 自定义事件内容分发 1. 第一个Vue程序
首先把vue.js拷贝到本地… 前端:Vue.js学习 1. 第一个Vue程序2. Vue指令2.1 v-if、v-else-if、v-else2.2 v-for2.3 事件绑定 v-on:2.4 v-model 数据双向绑定2.5 v-bind 绑定属性 3. Vue组件4. Vue axios异步通信5. 计算属性6. 插槽 slots7. 自定义事件内容分发 1. 第一个Vue程序
首先把vue.js拷贝到本地下载链接位vue.js 参考代码如下
!DOCTYPE html
html langen
headmeta charsetUTF-8meta nameviewport contentwidthdevice-width, initial-scale1.0titlevue学习/title
/head
bodydiv idapp{{message}}/div
/body
script typetext/javascript src./vue.js/script
script typetext/javascriptvar obj new Vue({el:#app,data:{message:hello world!}});
/script
/html运行结果 其中对象obj属性el表示绑定标签元素属性data表示数据。支持双向绑定(即当数据发生变化时视图也会发生变化当视图发生变化时数据也会跟着同步变化)简而言之就是在前端界面上打开控制台执行代码obj.message‘其他’,执行完成这句代码之后可以发现界面上显示为其他。
2. Vue指令
以v-开头的表示它是Vue提供的特殊特性如v-bind、v-if、v-else等。
2.1 v-if、v-else-if、v-else
参考示例如下
!DOCTYPE html
html langen
headmeta charsetUTF-8meta nameviewport contentwidthdevice-width, initial-scale1.0titlevue学习/title
/head
bodydiv idappdiv v-ifnum 11/divdiv v-else-ifnum 22/divdiv v-else其他数字/div/div
/body
script typetext/javascript src./vue.js/script
script typetext/javascriptvar obj new Vue({el:#app,data:{num:1}});
/script
/html上述指令用于判断展示哪个标签元素。如在一个登录界面上有多种登录方式同时对应着多个不同的输入框布局通过上述指令结合使用可以轻轻松松实现所想要的效果。
2.2 v-for
用于循环遍历Vue绑定的标签元素中的数据参考代码如下
!DOCTYPE html
html langen
headmeta charsetUTF-8meta nameviewport contentwidthdevice-width, initial-scale1.0titlevue学习/title
/head
bodydiv idappulli v-foritem in items{{item}}/li!-- li v-for(item,index) in items{{item}},{{index}}/li --/ul/div
/body
script typetext/javascript src./vue.js/script
script typetext/javascriptvar obj new Vue({el:#app,data:{num:1,items:[1,2,3,4,5]}});
/script
/html运行结果
2.3 事件绑定 v-on:
用于绑定事件如点击一个按钮弹出一个提示框如下
!DOCTYPE html
html langen
headmeta charsetUTF-8meta nameviewport contentwidthdevice-width, initial-scale1.0titlevue学习/title
/head
bodydiv idappbutton v-on:clickfun1点击我/button/div
/body
script typetext/javascript src./vue.js/script
script typetext/javascriptvar obj new Vue({el: #app,data: {message: hello world!},methods: {fun1:(){// alert(this.message);alert(obj.message);}}});
/script
/html运行结果 为了简化操作可以简写为绑定的事件如v-on:click简写为click。
2.4 v-model 数据双向绑定
示例为一个输入框在输入框中输入内容在另一个标签元素内的文本内容会随着输入的内容而变化。参考示例如下
!DOCTYPE html
html langen
headmeta charsetUTF-8meta nameviewport contentwidthdevice-width, initial-scale1.0titlevue学习/title
/head
bodydiv idappinput typetext v-modelmessage{{message}}/div
/body
script typetext/javascript src./vue.js/script
script typetext/javascriptvar obj new Vue({el: #app,data: {message: hello world!}});
/script
/html运行结果 另一个示例下拉框双向绑定参考代码如下
!DOCTYPE html
html langen
headmeta charsetUTF-8meta nameviewport contentwidthdevice-width, initial-scale1.0titlevue学习/title
/head
bodydiv idappselect v-modelmessageoption value1 checked1/optionoption value22/optionoption value33/option/select{{message}}/div
/body
script typetext/javascript src./vue.js/script
script typetext/javascriptvar obj new Vue({el: #app,data: {message: 1}});
/script
/html运行结果
2.5 v-bind 绑定属性
学过springboot themlefy模板的读者应该了解这个th:href、th:value、th:srcv-bind也就和上述那些实现的效果一样v-bind:href对应于th:href为了简化操作可以直接写成 :href。
3. Vue组件
为了复用(重复使用)从某些方面说也就是为了提升编写代码效率吧比如导航栏在很多界面上都需要可以单独把这部分写到一个vue文件内在其他哪些需要导航栏的html界面内导入这个(组件)即可。(下述示例没有写vue文件)
!DOCTYPE html
html langen
headmeta charsetUTF-8meta nameviewport contentwidthdevice-width, initial-scale1.0titlevue学习/title
/head
bodydiv idappulliuze v-foritem in items v-bind:vitem/liuze/ul/div
/body
script typetext/javascript src./vue.js/script
script typetext/javascriptVue.component(liuze,{props:[v],template: li{{v}}/li});var obj new Vue({el: #app,data: {items: [1,2,3,4,5]}});
/script
/html运行结果
4. Vue axios异步通信
axios主要作用实现ajax异步通信。
!DOCTYPE html
html langen
headmeta charsetUTF-8meta nameviewport contentwidthdevice-width, initial-scale1.0titlevue学习/titlestyle[v-clock]{display: none;}/style
/head
bodydiv idapp v-clockdiv{{info.code}}/divulli v-foritem in info.data.lista v-bind:hrefitem.url{{item.title}}/a/li/ul/div
/body
script typetext/javascript src./vue.js/script
script typetext/javascript src./axios.js/script
script typetext/javascriptvar obj new Vue({el: #app,data(){return {// 需要和json格式一致info:{code: null,data:{list: [{title: null,url: null}]}}}},mounted(){axios.get(https://blog.csdn.net/community/home-api/v1/get-business-list?page1size20businessTypeblogorderbynoMorefalseyearmonthusernameqq_45404396).then((result) {this.info result.data;});}// 钩子函数});
/script
/html运行结果
5. 计算属性
计算属性突出在属性首先它是一个属性其次这个属性是有计算的能力这里的计算指的是函数简单的说它就是一个能够将计算结果缓存起来的属性(将行为转换成静态的属性)可以当作为缓存。不能methods里的方法名同名。
!DOCTYPE html
html langenheadmeta charsetUTF-8meta nameviewport contentwidthdevice-width, initial-scale1.0titlevue学习/title
/headbodydiv idappcurrentTime: {{currentTime()}}br !-- 方法 --currentTime2: {{currentTime2}} !-- 属性 --/div
/body
script typetext/javascript src./vue.js/script
script typetext/javascriptvar obj new Vue({el: #app,methods:{currentTime:(){return new Date().getTime();}},// 计算属性computed:{// 里面写方法是一个属性 // 不能methods里的方法名同名currentTime2:(){return new Date().getTime();}}});
/script/html运行结果
6. 插槽 slots
关于组件如何接收模板内容下述是官网关于这个知识点的介绍。
参考代码
!DOCTYPE html
html langenheadmeta charsetUTF-8meta nameviewport contentwidthdevice-width, initial-scale1.0titlevue学习/title
/headbodydiv idappliuzeliuze-title slotliuze-title v-bind:titletitle/liuze-titleliuze-items slotliuze-items v-foritem in items v-bind:itemitem/liuze-items /liuze/div
/body
script typetext/javascript src./vue.js/script
script typetext/javascriptVue.component(liuze,{template:divslot nameliuze-title/slotulslot nameliuze-items/slot /ul /div});Vue.component(liuze-title,{props:[title],template:p{{title}}/p});Vue.component(liuze-items,{props:[item],template:li{{item}}/li});var obj new Vue({el: #app,data:{title:编程语言,items:[Java,Python,C,Go]}});
/script/html运行结果
7. 自定义事件内容分发
现在相把上述编程语言进行删除操作参考代码如下
!DOCTYPE html
html langenheadmeta charsetUTF-8meta nameviewport contentwidthdevice-width, initial-scale1.0titlevue学习/title
/headbodydiv idappliuzeliuze-title slotliuze-title v-bind:titletitle/liuze-titleliuze-items slotliuze-items v-for(item,index) in items v-bind:itemitem :indexindex removeremoveItem(index)/liuze-items /liuze/div
/body
script typetext/javascript src./vue.js/script
script typetext/javascriptVue.component(liuze,{template:divslot nameliuze-title/slotulslot nameliuze-items/slot /ul /div});Vue.component(liuze-title,{props:[title],template:p{{title}}/p});Vue.component(liuze-items,{props:[item,index],template:li{{item}} button clickremove删除/button/li,methods:{remove:function(index){this.$emit(remove,index);}}});var obj new Vue({el: #app,data:{title:编程语言,items:[Java,Python,C,Go]},methods:{removeItem:function(index){this.items.splice(index, 1);}}});
/script/html运行结果 文章转载自: http://www.morning.zlqyj.cn.gov.cn.zlqyj.cn http://www.morning.dnbkz.cn.gov.cn.dnbkz.cn http://www.morning.rrcxs.cn.gov.cn.rrcxs.cn http://www.morning.jqpq.cn.gov.cn.jqpq.cn http://www.morning.vehna.com.gov.cn.vehna.com http://www.morning.hwljx.cn.gov.cn.hwljx.cn http://www.morning.jwbnm.cn.gov.cn.jwbnm.cn http://www.morning.gpmrj.cn.gov.cn.gpmrj.cn http://www.morning.rdkqt.cn.gov.cn.rdkqt.cn http://www.morning.playmi.cn.gov.cn.playmi.cn http://www.morning.bfgbz.cn.gov.cn.bfgbz.cn http://www.morning.pqbkk.cn.gov.cn.pqbkk.cn http://www.morning.fglxh.cn.gov.cn.fglxh.cn http://www.morning.stflb.cn.gov.cn.stflb.cn http://www.morning.kwyq.cn.gov.cn.kwyq.cn http://www.morning.tndxg.cn.gov.cn.tndxg.cn http://www.morning.rlnm.cn.gov.cn.rlnm.cn http://www.morning.chxsn.cn.gov.cn.chxsn.cn http://www.morning.wdshp.cn.gov.cn.wdshp.cn http://www.morning.xwbld.cn.gov.cn.xwbld.cn http://www.morning.mrfr.cn.gov.cn.mrfr.cn http://www.morning.qgmwt.cn.gov.cn.qgmwt.cn http://www.morning.tntgc.cn.gov.cn.tntgc.cn http://www.morning.syrzl.cn.gov.cn.syrzl.cn http://www.morning.zycll.cn.gov.cn.zycll.cn http://www.morning.xzrbd.cn.gov.cn.xzrbd.cn http://www.morning.nbgfk.cn.gov.cn.nbgfk.cn http://www.morning.wrcgy.cn.gov.cn.wrcgy.cn http://www.morning.dmchips.com.gov.cn.dmchips.com http://www.morning.gwdkg.cn.gov.cn.gwdkg.cn http://www.morning.hydkd.cn.gov.cn.hydkd.cn http://www.morning.rywr.cn.gov.cn.rywr.cn http://www.morning.ksgjn.cn.gov.cn.ksgjn.cn http://www.morning.glxdk.cn.gov.cn.glxdk.cn http://www.morning.lgphx.cn.gov.cn.lgphx.cn http://www.morning.wtsr.cn.gov.cn.wtsr.cn http://www.morning.cdrzw.cn.gov.cn.cdrzw.cn http://www.morning.lrnfn.cn.gov.cn.lrnfn.cn http://www.morning.yrjym.cn.gov.cn.yrjym.cn http://www.morning.znnsk.cn.gov.cn.znnsk.cn http://www.morning.glcgy.cn.gov.cn.glcgy.cn http://www.morning.qstkk.cn.gov.cn.qstkk.cn http://www.morning.nydtt.cn.gov.cn.nydtt.cn http://www.morning.fbjqq.cn.gov.cn.fbjqq.cn http://www.morning.slfmp.cn.gov.cn.slfmp.cn http://www.morning.wqmyh.cn.gov.cn.wqmyh.cn http://www.morning.bkgfp.cn.gov.cn.bkgfp.cn http://www.morning.llsrg.cn.gov.cn.llsrg.cn http://www.morning.lpmdy.cn.gov.cn.lpmdy.cn http://www.morning.kqkmx.cn.gov.cn.kqkmx.cn http://www.morning.rfrxt.cn.gov.cn.rfrxt.cn http://www.morning.lcbt.cn.gov.cn.lcbt.cn http://www.morning.rksg.cn.gov.cn.rksg.cn http://www.morning.27asw.cn.gov.cn.27asw.cn http://www.morning.qydgk.cn.gov.cn.qydgk.cn http://www.morning.sryyt.cn.gov.cn.sryyt.cn http://www.morning.fldrg.cn.gov.cn.fldrg.cn http://www.morning.lblsx.cn.gov.cn.lblsx.cn http://www.morning.rpljf.cn.gov.cn.rpljf.cn http://www.morning.xtdms.com.gov.cn.xtdms.com http://www.morning.xrmwc.cn.gov.cn.xrmwc.cn http://www.morning.wkhfg.cn.gov.cn.wkhfg.cn http://www.morning.nlcw.cn.gov.cn.nlcw.cn http://www.morning.gbxxh.cn.gov.cn.gbxxh.cn http://www.morning.tstwx.cn.gov.cn.tstwx.cn http://www.morning.tdgwg.cn.gov.cn.tdgwg.cn http://www.morning.ljbpk.cn.gov.cn.ljbpk.cn http://www.morning.qyrnp.cn.gov.cn.qyrnp.cn http://www.morning.bnylg.cn.gov.cn.bnylg.cn http://www.morning.ghryk.cn.gov.cn.ghryk.cn http://www.morning.zqdhr.cn.gov.cn.zqdhr.cn http://www.morning.xkwrb.cn.gov.cn.xkwrb.cn http://www.morning.xphcg.cn.gov.cn.xphcg.cn http://www.morning.jcxgr.cn.gov.cn.jcxgr.cn http://www.morning.dkfb.cn.gov.cn.dkfb.cn http://www.morning.zlhzd.cn.gov.cn.zlhzd.cn http://www.morning.jjxnp.cn.gov.cn.jjxnp.cn http://www.morning.wjlnz.cn.gov.cn.wjlnz.cn http://www.morning.ydhmt.cn.gov.cn.ydhmt.cn http://www.morning.gbkkt.cn.gov.cn.gbkkt.cn