Vue.js
使用组件:
1.全局注册
<div id="id">
<my-component></my-component>
</div>
1.注册
Vue.component('my-component',{template:'<div>a b c d e f g Template !</div>'})
2.创建根实例
new Vue({ el:"#id" })
结果 :
<div id="id">
<my-component>a b c d e f g Template !</my-component>
</div>
2.局部注册
var Child = {
  template: '<div>A custom component!</div>'
}
new Vue({
  // ...
  components: {
    // <my-component> 将只在父组件模板中可用
    'my-component': Child
  }
})
class和style绑定
1.对象 语法<div v-bind:class="{active:isActive}"></div>
<div class="static" v-bind:class="{ active: isActive, 'text-danger': hasError }"> </div>
data:{
isActive:true,
hasError:false,
            'text-danger': false  <绑定的数据对象不必内联定义在模板里>
                  }
结果 <div class="static active"></div>
绑定一个返回对象的计算属性的方式实现
       <div v-bind:class="classObject"></div>
       data: {
           isActive: true,
           error: null
       },
      computed: {
       classObject: function () {
         return {
           active: this.isActive && !this.error,
           'text-danger': this.error && this.error.type === 'fatal'
          }
      }
    }
2.数组语法
<div v-bind:class="[activeClass,errorClass]"></div>
data:{
   activeClass:'active',
   errorClass:'text-danger'
}
结果:
<div class="active text-danger"></div>
有多个时,在数组语法中也可以使用对象语法 eg: <div v-bind:class="[{active,isActive},errorClass]"></div>
事件处理
1.事件处理方法
<div id="example-2">
<button v-on:click="FangFaMing" >FangFaMing</button>
</div>
var example2 =new Vue({
el:"#example-2",
data:{
name:'xiaoMing'
},
//在methods方法中定义方法
methods:{
FangFaMing:function(event){
alert('hello'+this.name+'!')
                // `event` 是原生 DOM 事件
               if(event){
                   alert(event.target.tagName)
               }
}
}
})
表单输入绑定
1.多行文本
<span>input message is </span>
<p style="white-space: pre-line;">{{ message }}</p>
<br>
<textarea v-model="message" placeholder="add multiple lines"></textarea>
2.复选框
多个复选框绑定要一个数组
<div id='example-3'>
<input type="checkbox" id="jack" value="Jack" v-model="checkedNames">
<label for="jack">Jack</label>
<input type="checkbox" id="john" value="John" v-model="checkedNames">
<label for="john">John</label>
<input type="checkbox" id="mike" value="Mike" v-model="checkedNames">
<label for="mike">Mike</label>
<br>
<span>Checked names: {{ checkedNames }}</span>
</div>
new Vue({ el: '#example-3', data: {
checkedNames: []
}
})
Checked names: [ "Jack", "John" ]
3.单选框
<div id="example-4">
<input type="redio" id="heng" v-model="picked" v-bind:value="哼">
<label for="heng">哼</label>
<br>
<input type="radio" id="ha" value="哈" v-model="picked">
     <label for="ha">哈</label>
  <br>
</div>
new Vue({
el:'example-4',
data:{
picked:''
}
})
哈
Picked: 哼
选择框 1,单选时
<div id="example-5">
  <select v-model="selected">
    <option disabled value="">请选择</option>
    <option>A</option>
    <option>B</option>
    <option>C</option>
  </select>
  <span>Selected: {{ selected }}</span>
</div>
new Vue({
  el: '...',
  data: {
    selected: ''
  }
}) 
 
<div id="example-6">
     text: 'One', value: 'A'
     },{
     text: 'Two', value: 'B'
    },{
     text: 'Three', value: 'C'
    }]// 单选框当选中时
vm.pick === vm.a
// 选择框当选中时
<select v-model="selected">
    <!-- 内联对象字面量 -->
  <option v-bind:value="{ number: 123 }">123</option>
</select>
typeof vm.selected // => 'object'
vm.selected.number // => 123
修饰符
。trim去空格,具体都可以参考API<input v-model.trim="msg">
 
 
                    
                 
                
            
         
 浙公网安备 33010602011771号
浙公网安备 33010602011771号