其他分享
首页 > 其他分享> > Vue.js期末考复习总结,看这一篇就够了

Vue.js期末考复习总结,看这一篇就够了

作者:互联网

第一章

MVVM 概念

Vue是渐进式JS框架,渐进式的意义是什么

单页面应用及其优缺点

第二章

创建Vue实例

var app = new Vue({...})

数据与方法

  var obj = {foo: '10'}
  Object.freeze(obj);
  new Vue({
    el: '#app',
    data: obj
  })

生命周期与钩子函数

计算属性

<div id="example">
  <p>Original message: "{{ message }}"</p>
  <p>Computed reversed message: "{{ reversedMessage }}"</p>
</div>
var vm = new Vue({
  el: '#example',
  data: {
    message: 'Hello'
  },
  computed: {
    // 计算属性的 getter
    reversedMessage: function () {
      // `this` 指向 vm 实例
      return this.message.split('').reverse().join('')
    }
  }
})

第三章

侦听属性

watch: {
  message: function(newValue, oldValue) {
    console.log("新" + newValue + "旧" + oldValue)
  }
}

过滤器

//全局过滤器
Vue.filter('captitalize', function(value){
  ...
})
//局部过滤器
falters: {
  capitalize: function(value) {
    ...
  }
}

第四章

基本指令

条件渲染

<div v-if="type === 'A'">
  A
</div>
<div v-else-if="type === 'B'">
  B
</div>
<div v-else-if="type === 'C'">
  C
</div>
<div v-else>
  Not A/B/C
</div>
<template v-if="loginType === 'username'">
  <label>Username</label>
  <input placeholder="Enter your username" key="username-input">
</template>
<template v-else>
  <label>Email</label>
  <input placeholder="Enter your email address" key="email-input">
</template>

列表渲染

<ul id="example-2">
  <li v-for="(item, index) in items">
    {{ parentMessage }} - {{ index }} - {{ item.message }}
  </li>
</ul>
var example2 = new Vue({
  el: '#example-2',
  data: {
    parentMessage: 'Parent',
    items: [
      { message: 'Foo' },
      { message: 'Bar' }
    ]
  }
})
<ul id="v-for-object" class="demo">
  <li v-for="value in object">
    {{ value }}
  </li>
</ul>
new Vue({
  el: '#v-for-object',
  data: {
    object: {
      title: 'How to do lists in Vue',
      author: 'Jane Doe',
      publishedAt: '2016-04-10'
    }
  }
})
<div id="app">
  <h3>没有报道的学生名单</h3>
  <ul>
    <li v-for="n in items" v-if="!n.value">{{n.name}}</li>
  </ul>
</div>
<script>
  var app = new Vue({
    el: '#app',
    data: {
        items: [
          {name: '小明',},
          {name: '小红', value:'已报道'},
          {name: '小华', value: '已报道'},
          {name: '小思',},
        ]
    }
  })
</script>

数组更新检测

自定义指令

<div id="app">
  <input v-focus></input>
</div>
<script>
//全局指令
Vue.directive('focus', {
    inserted: function(el) {
        el.focus
    }
})
//局部指令
var app = new Vue({
    el: '#app',
    directives: {
        focus: {//指令。。。}
    }
})
</script>

疑难困惑

第五章

绑定html样式

<style>
  .style1 {
    color: white;
  }
  .style2 {
    color: black;
  }
</style>
<div id="app">
    <div v-bind:class="{ 'style1', 'style2' }">{{message}}</div>
</div>
<script>
  new Vue({
    el: '#app',
    data: {
        message: '数组语法'
    }
  })
</script>
<style>
  .static {
    color: #3a8ee6;
  }
  .style1 {
    color: white;
  }
  .style2 {
    color: black;
  }
</style>
<div id="app">
  <div class="static" v-bind:class="{style1: boole1, 'style': boole2}"></div>
</div>
<script>
  new Vue({
    el: '#app',
    data: {
        boole1: true,
        boole2: true
    }
  })
</script>

绑定内联样式

<div id="app">
  <div v-bind:style="{color:'red', fontSize:'30'}">对象语法</div>
</div>
<script>
  new Vue({
    el: '#app',
  })
</script>
<div id="app">
  <div v-bind:style="[styleObj1, styleObj2]">对象语法</div>
</div>
<script>
  new Vue({
    el: '#app',
    data: {
      styleObj1:{
          color: 'blue'
      }
    },
    computed: {
        styleObj2:function (){
            return {
                padding: '30px'
            }
        }
    }
  })
</script>

第六章

事件处理

<div id="app">
  <div>单击:
    <button v-on:click="reduce(1)">-1岁</button>
    <button v-on:click="add(1)">+1岁</button>
  </div>
  <p>老王的年龄{{age}}岁</p>
  <div>双击:
    <button v-on:click="reduce(10)">-10岁</button>
    <button v-on:click="add(10)">+10岁</button>
  </div>
</div>
<script>
  new Vue({
    el: '#app',
    data: {
      age: 50
    },
    methods: {
        add:function (change){
            this.age-=change
        },
        reduce:function (change){
          this.age-=change
        },
    }
  })
</script>

事件修饰符

<div @click.stop="inside">阻止事件</div>

按键修饰符

<input @keyup.enter="submit">

系统修饰键

<!-- Alt + C -->
<input @keyup.alt.67="clear">

<!-- Ctrl + Click -->
<div @click.ctrl="doSomething">做一些操作</div>

疑难解惑

<button @click.ctrl.exact="onClick">A</button>

第七章

表单双向绑定

<input v-model="message" placeholder="edit me">
<p>Message is: {{ message }}</p>
<span>Multiline message is:</span>
<p style="white-space: pre-line;">{{ message }}</p>//pre-line的含义是合并行序列保留换行符
<br>
<textarea v-model="message" placeholder="add multiple lines"></textarea>
<input type="checkbox" id="checkbox" v-model="checked">
<label for="checkbox">{{ checked }}</label>
<div id="example-4">
  <input type="radio" id="one" value="One" v-model="picked">
  <label for="one">One</label>
  <br>
  <input type="radio" id="two" value="Two" v-model="picked">
  <label for="two">Two</label>
  <br>
  <span>Picked: {{ picked }}</span>
</div>
<script>
  new Vue({
    el: '#example-4',
    data: {
      picked: ''
    }
  })
</script>
<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>
<script>
  new Vue({
    el: '...',
    data: {
      selected: ''
    }
  })
</script>
多选时 (绑定到一个数组):

<div id="example-6">
  <select v-model="selected" multiple style="width: 50px;">
    <option>A</option>
    <option>B</option>
    <option>C</option>
  </select>
  <br>
  <span>Selected: {{ selected }}</span>
</div>
<script>
  new Vue({
    el: '#example-6',
    data: {
      selected: []
    }
  })
</script>

绑定选择框

<div id="app">
  <select v-model="selected" multiple>
    <option :value="{num:1}">A</option>
    <option :value="{num:2}">B</option>
    <option :value="{num:3}">C</option>
    <option :value="{num:4}">D</option>
  </select>
  <br>
  <span>Selected: {{ selected }}</span>
</div>
<script>
  new Vue({
    el: '#app',
    data: {
      selected: []
    }
  })
</script>

修饰符

<input v-model.lazy="msg"></input>

第八章

组件的注册

<div id="app">
  <component-a></component-a>
</div>
<script>
  //方法1
  Vue.component('component-a', { /* ... */ })
  //方法2
  var component = Vue.extend({
    template: '<div><h3>全局组件</h3></div>>'
  })
  Vue.component('component-a', component)

  new Vue({ el: '#app' })
</script>
<script>
  var ComponentA = { /* ... */ }
  var ComponentB = { /* ... */ }
  var ComponentC = { /* ... */ }
  new Vue({
    el: '#app',
    components: {
      'component-a': ComponentA,
      'component-b': ComponentB
    }
  })
</script>
<div id="app">
  <my-com2></my-com2>
</div>
<script>
  Vue.component('myCom2',Vue.extend({
    template: '<div>直接用component创建的组件</div>'
  }))
  new Vue({
    el: '#app'
  })
</script>

组件中data的选项

<div id="app">
  <my-com></my-com>
</div>
<template id="tmp">
  <h3>{{name}}</h3>
</template>
<script>
  Vue.component('my-com', {
      template: '#tmp',
      data: function (){
          return {
              name: 'john'
          }
      }
  })
  var app = new Vue({
    el: '#app'
  })
</script>

组件间数据通信

<div id="app">
  <h3>请输入要传的值<input type="text" v-model="title"></input></h3>
  <child-node v-bind:parentTitle="title"></child-node>
</div>
<template id="tmp">
    <div>
      <h4>属性值 {{content}} </h4>
    </div>
</template>
<script>
var app = new Vue( {
  el: '#app',
    data(){
        return {
          title:'首页',
        }
    },
    components:{
        'childNode': {
            template: '#tmp',
            props: ['parentTitle'],
            data() {
                return{
                    content: this.parentTitle
                }
            },
          watch: {
                parentTitle: {
                    this.content = this.parentTitle
                }
          },
        }
    }
})
</script>
<div id="app">
  <h3>属性值 {{msg}} </h3>
  <child-node @show="showMsg"></child-node>
</div>
<template id="tmp">
    <div>
        <h4>请输入要传的值<input type="text" v-model="childMsg"></input>
        <button @click="func">传递</button>
        </h4>
    </div>
</template>
<script>
var app = new Vue( {
  el: '#app',
    data(){
        return {
          title:'',
          msg: ''
        }
    },
    components:{
        'childNode': {
            template: '#tmp',
            data() {
                return{
                    childMsg: ''
                }
            },
          methods: {
                func() {
                    this.$emit('show', this.childMsg)
                }
          },
        }
    }
})
</script>

标签:el,期末考,Vue,app,就够,new,message,data
来源: https://blog.csdn.net/yuiyake/article/details/117885053