Vue(options)中,options包含五类属性css
el - 挂载点html
new Vue({ el:'#app' })
new Vue().$mount('#app')
data - 内部数据vue
methods - 方法node
components - 组件es6
用法:api
import Cpn form './Cpn.vue' ... new Vue({ components:{ Abc: Cpn } template:` <abc/> ` ... })
组件的其余引入方法:数组
页面内建立组件:缓存
//第一个参数为组件名,第二个参数是vue实例中的options Vue.component("Abc",{ template: "<div>n</div>" ... })
前两种的结合app
new Vue({ components:{ Abc:{ template:``, methods:{} ... } } })
生命钩子:框架
created(){...}
实例出如今内存中时调用mounted(){...}
//实例出如今页面中时调用updated(){...}
//实例数据更新时调用destroyed(){...}
//实例被销毁时调用使用方法:
//Demo组件内: export default{ props:['counter'] //也能够传回调函数 } //main.js内: template:` <Demo counter="1" /> `
注意:若是想给自定义组件的属性赋值变量,须要前边加冒号“:
”。例如:
//main.js内 ... data:{ n: 5 } template:`<Demo :counter:"n" />`
computed - 计算属性
语法:
data:{ firstName:'Oliver', lastName:'Young' } computed:{ //至关于只有get方法 Name(){ return this.firstName+this.lastName } }
computed:{ Name:{ get(){ return this.firstName+this.lastName } set(value){ this.firstName=value } } }
优势:
简化代码,避免重复,方便修改 自带缓存功能:若是依赖的属性没有变化,则不会从新计算
watch - 侦听
语法:
1. ``` watch:{ o1: function(newVal,oldVal){}, //newVal和oldVal是Vue传给你用的 o2(){}, //es6语法 o3:[f1,f2] //依次执行f1,f2 o4:{ handler(){//处理函数}, deep: true/false, //见下方解释 immediate: true/false //第一次是否执行 }, o5: 'callbackName', //回调函数,写在methods里 "obj.a": function(){} //侦听obj的属性 } ``` 2. `vm.$watch('obj',fun,{deep:true})`
什么叫“变化”?
obj:{num:1}
,从新赋值为obj:{num}:1
,分配了新的内存,因此变了。computed V.S watch
computed:
()
watch:
immediate:第一次渲染时,是否监听
template
三种写法
写在HTML里(vue完整版)
//index.html <div id="app" @click="add">{{n}}</div> //main.js const vm = new Vue({ el:'#app', data(){ return{ n:0 }}, methods:{ add(){ this.n+=1 } } })
写在options里(vue完整版)
//index.html <div id="app></div> //main.js const vm = new Vue({ el:'#app', template:` <div> {{n}} <button @click="add">+1</button> </div> `, data(){ return{ n:0 } }, methods:{ add(){ this.n+=1 } } })
配合.vue文件(vue运行时版)
//Demo.vue <template> <div> {{n}} <button @click="add">+1</button> </div> </template> <script> export default { data(){ return{ n:0 } }, methods:{ add(){...} } } </script> <style> //这里写css </style> //index.html <div id="app"></div> //main.js import Demo from './Demo' new Vue({ render: h => h(Demo) }).$mount('#app')
template 模板语法
表达式 — {{ }}
绑定属性 — v-bind:
当标签的属性须要使用data域中的数据时,有两种写法。例子以下:
data:{ className:'red' } ... <div :class="className"> //或 <div v-bind:class="className">
样式绑定 — :style
data:{ fontsize: 16, top: 10 } <div :style="{fontSize: fontsize+'px', 'padding-top': top+'px'}">
绑定事件 — v-on:
<button v-on:click="add">+1</button>
<button v-on:click="n+=1">n+1</button>
<button @click="n+=1">+1</button>
条件判断 — v-if
<div v-if="x>0">x大于0</div> <div v-else-if="x===0">x等于0</div> <div v-else>x小于0</div>
用法显而易见,很好理解
循环 — v-for
for(value,key) in 对象 / for (item,index) in 数组
<ul> <li v-for="(item,index) in arrary" :key="index"> {{index}}:{{item}} </li> </ul> <ul> <li v-for="(value,key) in arrary" :key="key"> {{key}}:{{value}} </li> </ul>
:key="index"
会有bug,最好不要用index看成key显示/隐藏 — v-show
<div v-show="n%2==0"> n是偶数 </div>
<div :style={display: n%2==0? 'block':'none' }> n是偶数 </div>
修饰符
v-on:{.keyCode| .keyAlias} .prevent, .stop 等等
<input @keypress.13="fn" />
:用户键盘输入回车时,调用fn<input @keypress.enter="fn" />
:输入回车,调用fn<a @click.prevent="fn"></a>
:阻止默认事件<a @click.stop="fn"></a>
:中止事件冒泡v-bind: .sync 等等
directives - 指令
v-on
, v-for
, v-if
等等v-xxx
两种写法:
Vue.directive('x',directiveOptions)
声明一个局部指令
new Vue({ ... directives:{ "x":directiveOptions } })
directiveOptions里有哪些属性?
指令的做用:
mixins - 混入
写法
//demo.js内 const demo = { data(){ return { a:0, ...//其余公共属性 } } methods:{ add(a){ return a+1 } ...//其余公共方法 } } //组件Demo1内 <script> import demo from "./demo.js" export default{ mixin:[demo] } </script> //组件Demo2内 <script> import demo from "./demo.js" export default{ mixin:[demo] } </script>
做用:
extends - 继承
provide/inject - 提供/注入
未完待续.