大家好,又见面了,我是全栈君。
Vue是一个MVVM(Model / View / ViewModel)的前端框架,相对于Angular来说简单、易学上手快,近两年也也别流行,发展速度较快,已经超越Angular了。比较适用于移动端,轻量级的框架,文件小,运行速度快。最近,闲来无事,所以学习一下Vue这个流行的框架,以备后用。
1 new Vue({ 2 el, // 要绑定的 DOM element 3 template, // 要解析的模板,可以是 #id, HTML 或某個 DOM element 4 data, // 要绑定的数据 5 computed, // 依赖于别的数据计算出来的数据, name = firstName + lastName 6 watch, // 监听方法, 监听到某一数据变化时, 需要做的对应操作 7 methods, // 定义可以在元件或模板內使用的方法 8 })
1.html
1 <div id="app"> 2 <p>{
{msg}}</p> 3 </div>
2.js
1 var app=new Vue({ 2 el:'#app',//标签的类名、id,用于获取元素 3 //以键值对的形式存放用到的数据成员 4 data:{ 5 msg:'显示的内容' 6 }, 7 //包含要用到的函数方法 8 methods:{ 9 } 10 });
这样js中msg的内容就会在p标签内显示出来。
利用bootstrap+vue实现简易留言板的功能,可以增加、删除,弹出模态框
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>简易留言板</title> 6 <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"> 7 <meta name="apple-mobile-web-app-capable" content="yes"> 8 <meta name="apple-mobile-web-app-status-bar-style" content="black"> 9 <style> 10 11 </style> 12 <link rel="stylesheet" href="../../node_modules/bootstrap/dist/css/bootstrap.min.css"> 13 <script src="../../node_modules/jquery/dist/jquery.min.js"></script> 14 <script src="../../node_modules/bootstrap/dist/js/bootstrap.min.js"></script> 15 16 <script src="../../node_modules/vue/dist/vue.js"></script> 17 <script> 18 window.onload=function(){ 19 new Vue({ 20 el:'#box', 21 data:{ 22 myData:[], 23 username:'', 24 age:'', 25 nowIndex:-100 26 }, 27 methods:{ 28 add:function(){ 29 this.myData.push({ 30 name:this.username, 31 age:this.age 32 }); 33 34 this.username=''; 35 this.age=''; 36 }, 37 deleteMsg:function(n){ 38 if(n==-2){ 39 this.myData=[]; 40 }else{ 41 this.myData.splice(n,1); 42 } 43 } 44 } 45 }); 46 }; 47 </script> 48 </head> 49 <body> 50 <div class="container" id="box"> 51 <form role="form"> 52 <div class="form-group"> 53 <label for="username">用户名:</label> 54 <
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/112288.html原文链接:https://javaforall.cn