首页
所有内容 前端 文章正文
网友投稿
836
2022-05-29
1. Vue简介
Vue.js(读音 /vjuː/, 类似于 view) 是一套构建用户界面的渐进式框架。 Vue 只关注视图层, 采用自底向上增量开发的设计。通过尽可能简单的 API 实现响应的数据绑定和组合的视图组件。不仅易于上手,还便于与第三方库或既有项目整合。另一方面,当与现代化的工具链以及各种支持类库结合使用时,Vue 也完全能够为复杂的单页应用提供驱动。
2.Vue.js 目录结构
这里是我们要开发的目录,基本上要做的事情都在这个目录里。里面包含了几个目录及文件:
assets: 放置一些图片,如logo等。
components: 目录里面放了一个组件文件,可以不用。
App.vue: 项目入口文件,我们也可以直接将组件写这里,而不使用 components 目录。
main.js: 项目的核心文件。
3. Vue语法
Vue.js 使用了基于 HTML 的模版语法,允许开发者声明式地将 DOM 绑定至底层 Vue 实例的数据。Vue.js 的核心是一个允许你采用简洁的模板语法来声明式的将数据渲染进 DOM 的系统。结合响应系统,在应用状态改变时, Vue 能够智能地计算出重新渲染组件的最小代价并应用到 DOM 操作上。
{{ message }}
var app = new Vue({ el: '#app', data: { message: 'Hello Vue!' } methods:add(){ })
Vue 构造器中有一个el 参数,它是 DOM 元素中的 id。改动全部在以上指定的 div 内,div 外部不受影响。data 用于定义属性。methods 用于定义的函数,可以通过 return 来返回函数值。{{ }} 用于输出对象属性和函数返回值。
1) 插值
数据绑定最常见的形式就是使用 {{...}}(双大括号)的文本插值。
使用 v-html 指令用于输出 html 代码。
2)指令
指令带有前缀 v-,以表示它们是 Vue 提供的特殊 attribute。可能你已经猜到了,它们会在渲染的 DOM 上应用特殊的响应式行为。
v-model 指令用来在 input、select、textarea、checkbox、radio 等表单控件元素上创建双向数据绑定,根据表单上的值,自动更新绑定的元素的值。
按钮的事件我们可以使用 v-on 监听事件,并对用户的输入进行响应。
条件判断使用 v-if 指令。可以用 v-else 指令给 v-if 添加一个 "else" 块。
使用 v-show 指令来根据条件展示元素。
v-for 指令需要以 site in sites 形式的特殊语法, sites 是源数据数组并且 site 是数组元素迭代的别名。
3)计算属性
计算属性关键词是 computed。例子如下:
Original message: "{{ message }}"
Computed reversed message: "{{ reversedMessage }}"
var vm = new Vue({ el: '#example', data: { message: 'Hello' }, computed: { // 计算属性的 getter reversedMessage: function () { // `this` 指向 vm 实例 return this.message.split('').reverse().join('') } } })
vm.reversedMessage 依赖于 vm.message,在 vm.message 发生改变时,vm.reversedMessage 也会更新。使用 methods 来替代 computed,效果上两个都是一样的,但是 computed 是基于它的依赖缓存,只有相关依赖发生改变时才会重新取值。而使用 methods ,在重新渲染的时候,函数总会重新调用执行。可以说使用 computed 性能会更好,但是如果你不希望缓存,你可以使用 methods 属性。
4)监听属性
监听属性 watch,我们可以通过 watch 来响应数据的变化。
5)样式绑定
class 与 style 是 HTML 元素的属性,用于设置元素的样式,我们可以用 v-bind 来设置样式属性。为 v-bind:class 设置一个对象,从而动态的切换 class。
6)条件渲染
v-if 指令用于条件性地渲染一块内容。这块内容只会在指令的表达式返回 truthy 值的时候被渲染。另一个根据条件展示元素的选项是 v-show 指令。v-if 是“真正”的条件渲染,因为它会确保在切换过程中条件块内的事件-和子组件适当地被销毁和重建。v-if 也是惰性的:如果在初始渲染时条件为假,则什么也不做——直到条件第一次变为真时,才会开始渲染条件块。相比之下,v-show 就简单得多——不管初始条件是什么,元素总是会被渲染,并且只是简单地基于 CSS 进行切换。一般来说,v-if 有更高的切换开销,而 v-show 有更高的初始渲染开销。因此,如果需要非常频繁地切换,则使用 v-show 较好;如果在运行时条件很少改变,则使用 v-if 较好。
7) 事件处理
v-on 可以接收一个定义的方法来调用。
Add 1
The button above has been clicked {{ counter }} times.
var example1 = new Vue({ el: '#example-1', data: { counter: 0 } })
在事件处理程序中调用一些非常常见的需求。尽管我们可以在方法中轻松实现这点,但更好的方式是:方法只有纯粹的数据逻辑,而不是去处理 DOM 事件细节。为了解决这个问题,Vue.js 为 v-on 提供了事件修饰符。之前提过,修饰符是由点开头的指令后缀来表示的。.stop、.prevent、.capture、.self、.once、
.passive。
...
在监听键盘事件时,我们经常需要检查详细的按键。Vue 允许为 v-on 在监听键盘事件时添加按键修饰符。Vue 提供了绝大多数常用的按键码的别名:.enter
.tab、.delete (捕获“删除”和“退格”键)、.esc、.space、.up、.down、.left、.right。
8)表单数据绑定
v-model 指令在表单控件 、 及 <select> 元素上创建双向数据绑定。它会根据控件类型自动选取正确的方法来更新元素。</p><p><input v-model="message" placeholder="edit me"> <p>Message is: {{ message }}</p></p><p>对于单选按钮,复选框及选择框的选项,v-model 绑定的值通常是静态字符串 (对于复选框也可以是布尔值)。我们可以把值绑定到 Vue 实例的一个动态 property 上,这时可以用 v-bind 实现,并且这个 property 的值可以不是字符串。</p><p><input type="checkbox" v-model="toggle" true-value="yes" false-value="no"> // 当选中时vm.toggle === 'yes' // 当没有选中时vm.toggle === 'no'</p><p>4. Vue路由</p><p>Vue路由允许我们通过不同的 URL 访问不同的内容。<router-link> 是一个组件,该组件用于设置一个导航链接,切换不同 HTML 内容。to 属性为目标地址, 即要显示的内容。</p><p><router-link> 相关属性:</p><p>5. Vue.js Ajax(axios)</p><p>使用 axios 来完成 ajax 请求。</p><p><div id="app"> <h1>网站列表</h1> <div v-for="site in info" > {{ site.name }} </div> </div> <script type = "text/javascript"> new Vue({ el: '#app', data () { return { info: null } }, mounted () { axios .get('https://www.runoob.com/try/ajax/json_demo.json') .then(response => (this.info = response)) .catch(function (error) { // 请求失败处理 console.log(error); }); }}) </script></p><p>vue</p><p>
<strong>版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。</strong>
</p></div>
<div class="article_footer clear">
<div class="fr tag">标签:<a href="https://www.huoban.com/news/tags-1610.html">前端</a>
<a href="https://www.huoban.com/news/tags-150.html">开发</a>
<a href="https://www.huoban.com/news/tags-1470.html">核心</a>
</div>
<div class="bdsharebuttonbox fl share">
<div class="share-widget fl">
<div class="social-share" data-sites="wechat,weibo, qq, qzone"></div>
</div>
</div>
</div>
<!-- 广告位ad4 -->
<div class="post-navigation clear">
<div class="post-previous fl">
<span>上一篇:</span><a href="https://www.huoban.com/news/post/6061.html">HarmonyOS(鸿蒙)——页面</a>
</div>
<div class="post-next fr">
<span>下一篇:</span><a href="https://www.huoban.com/news/post/6063.html">10分钟!一键部署Oracle 11GR2单机</a>
</div>
</div>
</div>
<div class="related_article">
<div class="box_title clear">
<span><i class="icon fa fa-paper-plane"></i>相关文章</span>
</div>
<div class="related_list clear">
<article class="fl">
<div class="related_img"><a href="https://www.huoban.com/news/post/72888.html"><img src="https://www.huoban.com/news/zb_users/cache/ly_autoimg/n/NzI4ODg.jpg"></a></div>
<div class="related_detail">
<h3><a href="https://www.huoban.com/news/post/72888.html" title="什么是低代码?低代码开发平台靠谱吗?低代码平台优缺点">什么是低代码?低代码开发平台靠谱吗?低代码平台优缺点</a></h3>
<div class="meta">
<span><i class="fa fa-eye"></i>836</span>
<span><i class="fa fa-clock-o"></i>2022-05-29</span>
</div>
</div>
</article>
<article class="fl">
<div class="related_img"><a href="https://www.huoban.com/news/post/72886.html"><img src="https://www.huoban.com/news/zb_users/cache/ly_autoimg/n/NzI4ODY.jpg"></a></div>
<div class="related_detail">
<h3><a href="https://www.huoban.com/news/post/72886.html" title="无代码软件发展简史及未来趋势,《无代码开发完整指南》完整版">无代码软件发展简史及未来趋势,《无代码开发完整指南》完整版</a></h3>
<div class="meta">
<span><i class="fa fa-eye"></i>836</span>
<span><i class="fa fa-clock-o"></i>2022-05-29</span>
</div>
</div>
</article>
<article class="fl">
<div class="related_img"><a href="https://www.huoban.com/news/post/72880.html"><img src="https://www.huoban.com/news/zb_users/cache/ly_autoimg/n/NzI4ODA.jpg"></a></div>
<div class="related_detail">
<h3><a href="https://www.huoban.com/news/post/72880.html" title="无代码开发不用编程就能开发软件,一文看懂:免费无代码开发软件“好在哪”,“怎么选”?">无代码开发不用编程就能开发软件,一文看懂:免费无代码开发软件“好在哪”,“怎么选”?</a></h3>
<div class="meta">
<span><i class="fa fa-eye"></i>836</span>
<span><i class="fa fa-clock-o"></i>2022-05-29</span>
</div>
</div>
</article>
</div>
</div>
<!--<p class="comment-disable sb br mb"><i class="iconfont icon-cry"></i>抱歉,评论功能暂时关闭!</p>-->
</div>
</div>
<div class="sidebar">
<div id="推荐文章" class="part clear 推荐文章">
<div class="top">
<h3 class="title">推荐文章</h3>
</div>
<div class="side 推荐文章"><ul><ul class="hot_posts"> <li><h4><a href="https://www.huoban.com/news/post/132763.html" title="企业生产管理是什么,企业生产管理软件">企业生产管理是什么,企业生产管理软件</a></h4></li><li><h4><a href="https://www.huoban.com/news/post/136160.html" title="盘点进销存软件排行榜前十名">进盘点进销存软件排行榜前十名</a></h4></li><li><h4><a href="https://www.huoban.com/news/post/132779.html" title="进销存系统哪个简单好用?进销存系统优点">进销存系统哪个简单好用?进销存系统优点</a></h4></li><li><h4><a href="https://www.huoban.com/news/post/133648.html" title="工厂生产管理(工厂生产管理流程及制度)">工厂生产管理(工厂生产管理流程及制度)</a></h4></li><li><h4><a href="https://www.huoban.com/news/post/132780.html" title="生产管理软件,机械制造业生产管理,制造业生产过程管理软件">生产管理软件,机械制造业生产管理,制造业生产过程管理软件</a></h4></li><li><h4><a href="https://www.huoban.com/news/post/132776.html" title="进销存软件和ERP有什么区别?进销存与erp软件理解">进销存软件和ERP有什么区别?进销存与erp软件理解</a></h4></li><li><h4><a href="https://www.huoban.com/news/post/132974.html" title="进销存如何进行库存管理">进销存如何进行库存管理</a></h4></li><li><h4><a href="https://www.huoban.com/news/post/132269.html" title="excel销售订单管理系统(销售订单录入系统)">如何利用excel制作销售订单管理系统?</a></h4></li><li><h4><a href="https://www.huoban.com/news/post/136946.html" title="数据库订单管理系统有哪些功能?数据库订单管理系统怎么设计?">数据库订单管理系统有哪些功能?数据库订单管理系统怎么设计?</a></h4></li><li><h4><a href="https://www.huoban.com/news/post/132312.html" title="数据库订单管理系统(订单系统数据流图)">什么是数据库管理系统?</a></h4></li></ul></ul></div>
</div>
<div id="divPrevious" class="part clear previous">
<div class="top">
<h3 class="title">最近发表</h3>
</div>
<div class="side divPrevious"><ul><li><a title="提升软件开发与招投标效率的五个方法与技术方案" href="https://www.huoban.com/news/post/157788.html">提升软件开发与招投标效率的五个方法与技术方案</a></li>
<li><a title="提升软件开发效率与招投标质量的五个方法" href="https://www.huoban.com/news/post/157787.html">提升软件开发效率与招投标质量的五个方法</a></li>
<li><a title="提升软件开发与招投标效率:AI智写助手如何优化智能文档编写" href="https://www.huoban.com/news/post/157786.html">提升软件开发与招投标效率:AI智写助手如何优化智能文档编写</a></li>
<li><a title="提升软件开发与招投标效率:AI智写助手如何实现智能文档编写" href="https://www.huoban.com/news/post/157785.html">提升软件开发与招投标效率:AI智写助手如何实现智能文档编写</a></li>
<li><a title="提升软件开发与招投标文档编写效率的AI智写助手" href="https://www.huoban.com/news/post/157784.html">提升软件开发与招投标文档编写效率的AI智写助手</a></li>
<li><a title="网店运营的关键从订单管理到用户体验" href="https://www.huoban.com/news/post/157783.html">网店运营的关键从订单管理到用户体验</a></li>
<li><a title="选择适合你的库存盘点软件指南" href="https://www.huoban.com/news/post/157782.html">选择适合你的库存盘点软件指南</a></li>
<li><a title="电商ERP解决方案助力企业实现业务飞跃" href="https://www.huoban.com/news/post/157781.html">电商ERP解决方案助力企业实现业务飞跃</a></li>
<li><a title="云ERP:企业高效管理与数字化转型的利器" href="https://www.huoban.com/news/post/157780.html">云ERP:企业高效管理与数字化转型的利器</a></li>
<li><a title="销售报表分析的秘密,掌握数据让业绩飞跃" href="https://www.huoban.com/news/post/157779.html">销售报表分析的秘密,掌握数据让业绩飞跃</a></li>
</ul></div>
</div>
<div id="sidebar_ad" class="part clear sidebar_ad">
<div class="part sidebar_ad"><div class="active"><a href='https://mrhnug.r.huobanbot.com/wxwork/pub/landings/426?app_id=1960&company_id=54&corp_id=wwbd4b7b6e7b0ccdaa&app_company_id=1' target='_blank'><img style='width:100%;height:100%' src='https://www.huoban.com/news/zb_users/upload/2023/08/erwei3.jpg'></a><br>
<a href='https://www.huoban.com/crm.html?utm=jiasouadv' target='_blank'><img style='width:100%;height:100%' src='https://www.huoban.com/news/zb_users/upload/2023/03/20230321171645167939020583758.png'></a><br>
</div></div>
</div>
<div id="hot_posts" class="part clear hot_posts">
<div class="top">
<h3 class="title">热评文章</h3>
</div>
<ul class="hot_posts"><li><h4><a href="https://www.huoban.com/news/post/104011.html" title="零代码开发是什么?2022低代码平台排行榜">零代码开发是什么?2022低代码平台排行榜</a></h4></li><li><h4><a href="https://www.huoban.com/news/post/131019.html" title="智能进销存库存管理系统(智慧进销存)">智能进销存库存管理系统(智慧进销存)</a></h4></li><li><h4><a href="https://www.huoban.com/news/post/73907.html" title="在线文档哪家强?8款在线文档编辑软件推荐">在线文档哪家强?8款在线文档编辑软件推荐</a></h4></li><li><h4><a href="https://www.huoban.com/news/post/102663.html" title="WPS2016怎么绘制简单的价格表?">WPS2016怎么绘制简单的价格表?</a></h4></li><li><h4><a href="https://www.huoban.com/news/post/152072.html" title="家居定制平台是什么?">家居定制平台是什么?</a></h4></li><li><h4><a href="https://www.huoban.com/news/post/55539.html" title="连锁餐饮管理系统的功能有哪些?餐饮服务系统的构成及工作程序">连锁餐饮管理系统的功能有哪些?餐饮服务系统的构成及工</a></h4></li></ul> </div>
<div id="divLinkage" class="part clear link">
<div class="top">
<h3 class="title">友情链接</h3>
</div>
<div class="side divLinkage"><ul><li class="link-item"><a href="https://www.huoban.com/" target="_blank" title="伙伴云">伙伴云</a></li><li class="link-item"><a href="https://www.huoban.com/news/category-19.html" title="进销存管理">进销存管理</a></li><li class="link-item"><a href="https://www.huoban.com/news/category-3.html" title="低代码">低代码</a></li><li class="link-item"><a href="https://www.huoban.com/news/tags-12.html" target="_blank" title="Excel表格">Excel表格</a></li><li class="link-item"><a href="http://www.lvyuanlin.net/" title="海威行">海威行</a></li><li class="link-item"><a href="https://www.finclip.com/news/" title="FinClip">FinClip</a></li></ul></div>
</div> </div>
</div>
</section>
</div>
<footer class="p-footer">
<div class="contant_box">
<div class="discover_tmt">
<h5 class="" style="font-size: 1px; color: white;">伙伴云</h5>
<div class="text_box">
<a href="https://www.jiasou.cn/article/" title="toB数字化营销SEO" style="font-size: 1px; color: white;">加搜toBSEO</a>
<a href="https://www.finclip.com/news/category-1.html" title="小程序工具" style="font-size: 1px; color: white;">前端框架</a>
<a href="https://www.jia-ai.com/info/" title="小红书营销攻略" style="font-size: 1px; color: white;">小红书营销攻略</a>
<a href="https://www.yanyin.tech/cms/" title="生物研究资讯" style="font-size: 1px; color: white;">生物研究资讯</a>
<a href="https://www.finclip.com/news/" title="FinClip 技术文档" style="font-size: 1px; color: white;">小程序容器帮助中心</a>
<a href="https://www.finclip.com/news/article/" title="小程序开发行业洞察" style="font-size: 1px; color: white;">小程序开发行业洞察</a>
<a href="https://www.foneplatform.com/jscms/" title="全面预算管理资讯" style="font-size: 1px; color: white;">全面预算管理资讯</a>
<a href="https://www.weiling.cn/article/" title="企微SCRM客户管理干货" style="font-size: 1px; color: white;">企微SCRM客户管理干货</a>
<a href="https://www.marketup.cn/info/" title="营销管理系统资讯" style="font-size: 1px; color: white;">营销管理系统资讯</a>
<a href="https://www.transfertech.cn/news/" title="制造行业新闻动态" style="font-size: 1px; color: white;">制造行业新闻动态</a>
</div> </div>
<div class="collaboration_box">
</div>
<div class="we_img_box clear">
<div class="img_box">
<img src="https://www.huoban.com/news/zb_users/theme/zblog5_news/image/ewm.png" alt="" class="hover_tmt">
</div>
</div>
</div>
<p class="info"> <a href="https://beian.miit.gov.cn" target="_blank" rel="nofollow">京ICP备12038259号</a>
<span>
<a href="#"></a></span>
</p>
</footer>
<div id="backtop" class="backtop">
<div class="bt-box top">
<i class="fa fa-angle-up fa-2x"></i>
</div>
</div>
<script charset="UTF-8" src="https://www.huoban.com/assets/js/sensorsdata.1.22.2.min.js"></script>
<script charset="UTF-8">
var sensors = window['sensorsDataAnalytic201505'];
sensors.init({
server_url: 'https://saapi.huoban.com/sa?project=production',
heatmap:{scroll_notice_map:'not_collect'},
use_client_time:true,
send_type:'beacon'
});
sensors.quick('autoTrack');
</script>
<script>
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "https://hm.baidu.com/hm.js?6444c045836d6bf27124085a4f62c2a8";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
</script>
<script>
(()=>{const e="https://analyze.jiasou.cc/api/v1/page_view/report/",n="9fe06d4884e0461caaa1de5651164d43";let t=null;const o=new Proxy({},{get:(e,n)=>localStorage.getItem(window.btoa(n)),set:(e,n,t)=>!!t&&(localStorage.setItem(window.btoa(n),t),!0)});new Promise((t=>{if(o.fingerprint)t();else{const a=function(){var e={};if(e.userAgent=navigator.userAgent||"",e.plugins=[],navigator.plugins&&navigator.plugins.length>0)for(var n=0;n<navigator.plugins.length;n++){var t={name:navigator.plugins[n].name||"",filename:navigator.plugins[n].filename||"",description:navigator.plugins[n].description||""};e.plugins.push(t)}e.languages=navigator.languages||[navigator.language||""],e.timezone=(new Date).getTimezoneOffset(),e.screenResolution={width:window.screen.width||0,height:window.screen.height||0,pixelDepth:window.screen.pixelDepth||0,colorDepth:window.screen.colorDepth||0};var o=document.createElement("canvas").getContext("2d"),a=[],i=["monospace","sans-serif","serif"];for(n=0;n<i.length;n++){var r=i[n];o.font="12px "+r,o.measureText("abcdefghijklmnopqrstuvwxyz0123456789").width>0&&a.push(r)}return e.fonts=a,e.cookieEnabled=navigator.cookieEnabled||!1,e.localStorage=void 0!==window.localStorage,e.sessionStorage=void 0!==window.sessionStorage,e.doNotTrack="1"===navigator.doNotTrack||"1"===window.doNotTrack||"1"===navigator.msDoNotTrack||"yes"===navigator.doNotTrack,e}();fetch(`${e}u/`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({key:n,f:window.btoa(JSON.stringify(a))})}).then((e=>{console.debug("browser fingerprint sent"),200===e.status&&e.json().then((e=>{console.debug("browser fingerprint received",e),o.fingerprint=e.fp,t()}))}))}})).then((()=>{e&&o.fingerprint&&fetch(e+`?${new URLSearchParams({token:n}).toString()}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({c:window.btoa(JSON.stringify({u:o.fingerprint,l:window.location.href,r:document.referrer}))})}).then((e=>{200==e.status&&e.json().then((e=>{e.track_id&&(t=e.track_id)}))}))})),window.addEventListener("beforeunload",(async n=>{t&&fetch(e+`?${new URLSearchParams({track_id:t}).toString()}`,{method:"GET",headers:{"Content-Type":"text/plain"},keepalive:!0}),n.returnValue=""}))})();
</script><script language="javascript" src="https://www.huoban.com/news/zb_users/plugin/ZF_ad/js/index.js?id=111"></script>
<script language="javascript" src="https://www.huoban.com/news/zb_users/plugin/ZF_ad/js/ZF_ad__cookie.js"></script>
</body>
</html>
<!--118.85 ms , 17 queries , 3691kb memory , 0 error-->