<!DOCTYPEhtml><html><head><metacharset="utf-8"><title></title><!-- 1.导入vue --><scriptsrc="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script></head><body><!-- 2.定义一个 标签 需要给一个标签添加 id --><divid="app"><span>{{message}}</span></div></body><!-- 3.创建vue实例 --><scripttype="text/javascript">var vm =newVue({el:'#app',data:{message:'hello',}})</script></html>
2.v-bind 绑定元素属性
<!DOCTYPEhtml><html><head><metacharset="utf-8"><title></title><scriptsrc="https://cdn.jsdelivr.net/npm/vue"></script></head><body><divid="app"><ahref="#">{{message}}</a><span>{{hello}}</span><hr><!-- <span>{{world}}</span> --><hr><av-bind:href="url">戳我有惊喜</a><br><span:title='showmessage'>鼠标放在这里</span></div></body><scripttype="text/javascript">var vm =newVue({el:'#app',data:{message:'领取优惠劵',hello:'hi en heng',url:'http://www.itheima.com',showmessage:'当前的时间是'+newDate().toLocaleString()}})</script></html>
3.v-for 循环
<!DOCTYPEhtml><html><head><metacharset="utf-8"><title></title><!-- 1.导入vue --><scriptsrc="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script></head><body><!-- 2.定义一个 标签 需要给一个标签添加 id --><divid="app"><span>{{message}}</span><hr><ul><liv-for="(item,index) in items"> {{index}} ~~~ {{item}} </li></ul><hr><!-- 对对象进行遍历 --><ul><liv-for="(value,key) in object">{{key}} {{value}}</li></ul><hr><ul><liv-for="todo in todos">{{todo.title}} ---- {{todo.author}}</li></ul></div></body><!-- 3.创建vue实例 --><scripttype="text/javascript">var vm =newVue({el:'#app',data:{message:'hello',items:['python','mysql','linux','html','js','css'],object:{title:'How to do lists in Vue',author:'Jane Doe',publishedAt:'2016-04-10'},todos:[{title:'Vue',author:'Jane Doe',publishedAt:'2016-04-10'},{title:'python',author:'Ricky',publishedAt:'2019-04-10'},{title:'itcast',author:'itcast',publishedAt:'2006-05-08'}]}})</script></html>
4.v-if
<!DOCTYPEhtml><html><head><metacharset="utf-8"><title></title><!-- 1.导入vue --><scriptsrc="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script></head><body><!-- 2.定义一个 标签 需要给一个标签添加 id --><divid="app"><span>{{message}}</span><hr><ahref="#"v-if="isLogin">欢迎你归来</a><hr><ahref="#"v-if="level === 1">青铜</a><ahref="#"v-else-if="level === 2">白银</a><ahref="#"v-else>王者</a><hr><spanv-if="seen">v-if</span><br><spanv-show="seen">v-show</span></div></body><!-- 3.创建vue实例 --><scripttype="text/javascript">var vm =newVue({el:'#app',data:{message:'hello',isLogin:false,level:2,seen:true,}})</script></html>
5.methods事件 v-on:绑定事件
可以用v-on指令监听 DOM 事件,并在触发时运行一些 JavaScript 代码。
<!DOCTYPEhtml><html><head><metacharset="utf-8"><title></title><!-- 1.导入vue --><scriptsrc="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script></head><body><!-- 2.定义一个 标签 需要给一个标签添加 id --><divid="app"><span>{{message}}</span><buttonv-on:click="login">登陆</button><br><ahref="javascript:;"@click="register">注册</a><hr><button@click="add(counter)">点击+1</button></div></body><!-- 3.创建vue实例 --><scripttype="text/javascript">var vm =newVue({// 接管标签el:'#app',// 绑定数据data:{message:'hello',counter:1,total:0,},// 方法methods:{login:function(){alert('我被点击了')},register:function(){alert('zhuce')},add:function(counter){// this 表示当前的 vue 我们通过this.total 来获取data中的变量this.total+=counter;alert(this.total)}}})</script></html>