博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Vue.js
阅读量:4358 次
发布时间:2019-06-07

本文共 2743 字,大约阅读时间需要 9 分钟。

 

使用组件:

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.复选框

    多个复选框绑定要一个数组 

Checked names: { { checkedNames }}
new Vue({
el: '#example-3', data: {
checkedNames: [] } })  Jack  John  Mike  Checked names: [ "Jack", "John" ] 3.单选框 
    
 
 new Vue({
el:'example-4', data:{
picked:'' } })  哼   哈 Picked: 哼
选择框  1,单选时
Selected: { { selected }}
new Vue({
el: '...', data: {
selected: '' } })
 
请选择   A   B   C Selected: B
 
多选时
<select v-model="selected"> 
    <option v-for="option in options" v-bind:value=“option.value”>
            {
{ option.text }}
   <option>
</select>
<span>Selected:{
{ selected }}</span>
  </div>
new Vue({
    el:'example-6',
    selected:'A',
    options:[{
text: 'One', value: 'A'
     },{
text: 'Two', value: 'B'
    },{
text: 'Three', value: 'C'
    }]
})
       One            Two            Three      Selected: A
 
值绑定
// 单选框当选中时 vm.pick === vm.a
// 选择框当选中时
typeof vm.selected // => 'object' vm.selected.number // => 123 修饰符 。trim去空格,具体都可以参考API
 
 

转载于:https://www.cnblogs.com/tianlifitting/p/8258958.html

你可能感兴趣的文章
Python第三方库jieba(中文分词)入门与进阶(官方文档)
查看>>
【转】eclipse for java ee的tomcat配置(常见问题解决)
查看>>
QQMacMgr for Mac(腾讯电脑管家)安装
查看>>
特效:ListBox数据加载特效
查看>>
php分页功能limit
查看>>
PHP中开启和关闭错误信息的提示
查看>>
C++ 中的一些错觉
查看>>
xshell配色方案
查看>>
使用注解实现 bean 转 csv
查看>>
iOS相机去黑框
查看>>
eclipse+webservice开发实例
查看>>
【bzoj2002】弹飞绵羊——分块
查看>>
php读取数据库数据,出现中文乱码(数据库中没有出现乱码)
查看>>
selenium动作链
查看>>
敏捷外包工程系列之二:人员结构(敏捷外包工程,敏捷开发,产品负责人,客户价值)...
查看>>
《设计你的人生》的部分经典语录
查看>>
mustache多次渲染和多个赋值
查看>>
《Flutter 实战》开源电子书
查看>>
Python 键盘记录
查看>>
HDU 1381 Crazy Search
查看>>