VUEJS
<div id="app">
<button @click="add">按钮</button>
<button @click="change">改变</button>
<ol id="list">
<li v-for="item in arr">{{item.text}}</li>
</ol>
<p>{{msg}}</p>
<p>{{arr}}</p>
<p>{{json}}</p>
</div>
new Vue({
el:"#app",
data:{
msg:"Hello",
arr:[{
text:"大前端"
},{
text:"Java"
},{
text:"安卓"
}],
json:{a:"appple",b:"banana"}
},
created:function(){
setTimeout(() => {
console.log("延迟下");
this.msg = "Hello";
},2000);
console.log(document.getElementById("app").innerHTML);
console.log("created");
},
methods:{
add:function(){
// 原生写法
// document.getElementById("list").innerHTML += "<li>1</li>"
this.arr.push({text: Math.random()});
this.change();
},
change:function(){
this.arr[1].text = "前端部门";
}
},
watch:{
a:function(val, oldVal){
console.log('new: ',val,' old:', oldVal)
}
},
beforeCreate:function(){
debugger
console.log("before");
},
mounted:function(){
console.log("计算属性");
}
});
<li v-for="item in arr">{{item.text}}</li>
: for循环li标签,arr为数据源,item为列表里等每一项 <div id="app">
<div><input type="text" @keydown="show()"/></div>
<div><input type="text" @keydown="showCode($event)"/></div>
<div><input type="text" @keydown.13="showEnter()"/></div>
<div><input type="text" @keydown.enter="showEnter()"/></div>
</div>
new Vue({
el: "#app",
data: {
},
methods: {
show: function () {
console.log("哈哈");
},
showCode:function(ev){
if(ev.keyCode==13){
console.log("您按回车了");
}
console.log(ev.keyCode);
},
showEnter:function(){
alert("您按回车了");
}
}
});
<div id="app">
<p :title="msg" :style="{color:'red'}">文字。。。。。。</p>
<p :title="msg" :style="[obj,bg]">文字。。。。。。</p>
<p :title="msg" :style="more">文字。。。。。。</p>
</div>
new Vue({
el:"#app",
data:{
msg:"信息",
obj:{color:'red'},
bg:{background:"blue"},
more:{
color:"red",
background:"gray"
}
}
});
:
号###自定义指令
<div id="app">
<strong v-red>红色文字</strong>
<strong v-red>红色文字2</strong>
<strong v-red>红色文字3</strong>
<input v-focus>
</div>
Vue.directive("red", function (el, tags) {
el.style.color = "red";
el.style.background = "green";
});
Vue.directive('focus', {
inserted: function (el) {
el.focus()
}
})
directive:注册指令入口。本例展示了全局注册指令的方式,只要节点绑定了对应的指令,就会执行指令的方法
new Vue({
el: "#app",
data: {},
directives: {
focus: {
inserted: function (el,binding,vnode,oldVnode) {
el.focus();
}
}
}
});
directive:注册指令入口。本例属于局部注册,只能在当前实例当中注册
###组件
<div id="app">
<p>父级:{{str}}</p>
<child-com :msg="str"></child-com>
<child-com :msg="number"></child-com>
</div>
<template id="child">
<div>
<p>我是子组件对象</p>
<input type="button" value="按钮" @click="change()"/>
<strong>{{msg}}</strong>
</div>
</template>
new Vue({
el:"#app",
data:{
str:"我是父组件对象"
},
components:{
"child-com":{
template:"#child",
props:['msg'],
methods:{
change(){
this.msg = "子对象里面的内容被改变了"
}
}
}
}
})