opitions :五种属性

红色:必学
黄色:进阶属性

紫色:特殊
蓝色:不常用,可学可不学
绿色:红色的对应
灰色:很不常用
数据:data props computed methods watch propsData
DOM:el template render renderError
生命周期钩子:beforeCreate create beforeMount mounted beforeUpdata updated beforeDestory destoryed actived deactivated errorCaptured
资源:directived filters components
组合:mixins extends provide inject parent

入门属性

el - 挂载点 与 $mount() 功能相同

  1. new Vue(opitions).$mount('selector')

data - 内部数据

  • 单文件组件只能用 function (避免组件复用时不同组件修改 data 时 data 被覆盖)

    1. new Vue({data(){return {n:0}}}) //优先使用函数
    2. new Vue({data:{n:0}})

    methods - 事件处理函数或普通函数

    components - 组件 (实例中的实例)

    1. import Demo from './Demo.vue'
    2. new Vue({
    3. components:{Demo}
    4. template:
    5. `<div>
    6. <Demo/>
    7. </div>`
    8. })
    9. Vue.components('demo',{template:'<div>demo</div>'})

    四个钩子

  • created - 实例出现在内存中时触发

  • mounted - 实例出现在页面中时触发
  • updated - 数据更新时触发
  • destoryed - 实例从页面中消失时触发


    props (外部属性) - 将外部变量传到组件内部使用,还可以传方法改变数据并更新组件视图

    1. //Demo.Vue
    2. <template>
    3. <div>{{data}}<div/>
    4. <template/>
    5. <srcipt>
    6. export default {
    7. props:['data','fn']
    8. }
    9. <script/>
    10. //main.js
    11. new Vue({
    12. data:{n:0}
    13. components:{Demo}
    14. template:
    15. `<div>
    16. <Demo data='你好 props' /> //字符串
    17. <Demo :data=' n ' /> //加 : 后表示js代码,变量 n
    18. <Demo :data=' n ' :fn=' add ' /> //传 add 方法,改变数据并重新渲染
    19. </div>`
    20. })
    21. methods:{
    22. add(){this.n += 1}
    23. }
    24. })

    进阶属性

    computed - 计算属性

  • 类型:{ [key: string]: Function | { get: Function, set: Function } }

  • 示例

    1. new Vue({
    2. data: {
    3. user: {
    4. email: " 123@qq.com",
    5. nickname: "圆圆",
    6. phone: "13812312312",
    7. },
    8. },
    9. computed: {
    10. displayName: {
    11. get() {
    12. const user = this.user;
    13. return user.nickname || user.email || user.phone;
    14. },
    15. set(value) {
    16. console.log();
    17. this.user.nickname = value;
    18. },
    19. },
    20. },
    21. template: `<div>
    22. {{displayName}}
    23. <div>
    24. {{displayName}}
    25. <button @click="add">改名</button>`,
    26. methods: {
    27. add() {
    28. this.displayName = "芳芳";
    29. },
    30. },
    31. }).$mount("#app");
  • 缓存:如果依赖的属性没有变化就不会重新计算。

    watch - 监听(异步):适合当数据变化时执行一个函数,或保存某些数据,不需要得到计算结果。(不要用箭头函数)

  • 语法:

    1. //语法1
    2. watch: {
    3. o1: function (value, oldvalue){},
    4. o2(){},
    5. o3: [f1,f2],
    6. o4: 'methodName',
    7. o5: {handler: fn, deep: true, immediate: true,}
    8. object.a": function(){},
    9. }
    10. //语法2
    11. create(){
    12. this.$watch('xxx', fn, {deep:true,immediate:true})
    13. }
    14. 其中xx可以改为一个返回字符串的函数
  • 示例:

    1. new Vue({
    2. data: {
    3. n: 0,
    4. history: [],
    5. inUndoMode: false,
    6. },
    7. watch: {
    8. n(newValue, oldValue) {
    9. if (!this.inUndoMode) {
    10. this.history.push({ from: oldValue, to: newValue });
    11. }
    12. },
    13. },
    14. template: `<div>
    15. <button @click="add">+1</button>
    16. <button @click="minus">-1</button>
    17. <hr/><button @click="Undo">撤销</button>
    18. <hr/>{{history}}
    19. </div>`,
    20. methods: {
    21. add() {
    22. this.n += 1;
    23. },
    24. minus() {
    25. this.n -= 1;
    26. },
    27. Undo() {
    28. const last = this.history.pop();
    29. const old = last.from;
    30. this.inUndoMode = true;
    31. this.n = old;
    32. this.$nextTick(() => { //因为 watch 是异步的所以需要使用 $nextTick
    33. this.inUndoMode = false;
    34. }, 0);
    35. },
    36. },
    37. }).$mount("#app");
  • immediate 属性:第一次是否执行

    1. new Vue({
    2. data:{
    3. obj:{a:0,b:1}
    4. },
    5. template:`<div>
    6. <button @click="obj.a += 1">obj.a + 1</button> //obj.a 数据变了,不会触发 obj
    7. <button @click="obj={a:0,b:1}">obj = 新对象</button> //只触发 obj
    8. </div>`,
    9. watch:{
    10. obj(){console.log('obj 数据变了')},
    11. "obj.a"(){console.log('obj.a 数据变了')},
    12. "obj.b"(){console.log('obj.b 数据变了')}
    13. }
    14. })
    15. //使用 immediate
    16. watch:{
    17. obj:{
    18. handle(){console.log('obj 数据变了'}),
    19. immediate:true //页面第一次加载时就会触发
    20. },
    21. }
  • deep 属性:监听对象时,是否监听对象内部的变化。默认如果对象地址没变,就不会触发绑定的事件

    1. new Vue({
    2. data:{
    3. obj:{a:0,b:1}
    4. },
    5. template:`<div>
    6. <button @click="obj.a += 1">obj.a + 1</button> //obj.a 数据变了,不会触发 obj
    7. <button @click="obj={a:0,b:1}">obj = 新对象</button> //只触发 obj
    8. </div>`,
    9. watch:{
    10. obj(){console.log('obj 数据变了')},
    11. "obj.a"(){console.log('obj.a 数据变了')},
    12. "obj.b"(){console.log('obj.b 数据变了')}
    13. }
    14. })
    15. //使用 deep
    16. watch:{
    17. obj:{
    18. handle(){console.log('obj 数据变了'}),
    19. deep:true //此时 obj 中的任何值改变都会触发事件
    20. },
    21. }

    directives - 自定义指令

  • 作用:减少重复的原生 DOM 操作

  • 全局指令:Vue.directive( 'x',{} )

    1. Vue.directive('focus', {
    2. bind: function(){}, // 只调用一次,指令第一次绑定到元素时调用,初始化设置
    3. inserted: function(el){ el.focus() }, // 当被绑定的元素插入到 DOM 中时
    4. unbind: function(){} // 只调用一次,指令与元素解绑时调用。
    5. })
  • 局部指令:options.directives:{}

    1. new Vue({
    2. el:"#xxx",
    3. data(){},
    4. directives: {
    5. focus: {
    6. inserted: function(el) { el.focus() }
    7. }
    8. }
    9. })

    mixins - 混入(复制,并智能合并)

  • 作用:减少 data、methods、钩子的重复。

  • 全局指令:Vue.mixin({}}
  • 局部指令:options.mixins:[ mixins1,mixins2 ]
  • 示例:

    1. //xxx.js
    2. const obj = {
    3. data(){},
    4. created(){console.log(1)} //1
    5. }
    6. export default obj
    7. //A.Vue
    8. import Obj from './xxx.js'
    9. <script>
    10. export default {
    11. created(){console.log(2)},
    12. mixins: [Obj] //1 2
    13. }
    14. </script>

    extends - 继承

  • 全局继承: Vue.extend({})

  • 局部继承:options.extends:{}

    1. //MyVue.js
    2. import Obj from './xxx.js'
    3. const MyVue = Vue.extend({
    4. mixins: [Obj]
    5. })
    6. //A.Vue
    7. import MyVue from './MyVue.js'
    8. <script>
    9. export default {
    10. created(){console.log(2)},
    11. extends: MyVue
    12. }
    13. </script>

    provide、inject - 提供、注入

  • 作用: 共用 data、methods,祖先提供,后代注入。

  • 示例:
    1. //父组件
    2. <script>
    3. export default {
    4. data(){ return {name : "圆圆"} },
    5. methods:{
    6. rename(){
    7. this.name = this.name === '圆圆' ? 芳芳 : 圆圆
    8. }
    9. },
    10. provide(){
    11. return {
    12. name: this.name,
    13. rename: this.rename
    14. }
    15. }
    16. }
    17. </script>
    18. //子组件
    19. <template>
    20. <div><button @click="rename"/></div>
    21. </template>
    22. <script>
    23. export default {
    24. reject:['name','rename']
    25. }
    26. </script>