Blogger Information
Blog 18
fans 1
comment 0
visits 17183
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Vue挂载点理解及常用属性
α清尘
Original
4003 people have browsed it

Vue挂载点

vue.js是什么?

  • vue.js是一套用于构建用户界面的渐进式框架;
  • vue只关注视图层,采用自底层向上逐层应用的设计;

vue挂载点

简单来说,挂载点是vue实例要去处理的dom节点,el属性声明;全称为element,声明挂载点来确定vue对象的作用域;从而对该元素及后代元素进行vue解析。将创建的vue实例在html文档中显示出来,这就是挂载。

创建一个vue实例:

  1. <div class="app">{{message}}</div>
  2. <script>
  3. const vm=new Vue({
  4. // 首先声明挂载点
  5. el:".app",
  6. // 声明挂载点中的数据对象
  7. data:{
  8. message:"hello world!"
  9. },
  10. });
  11. console.log(vm.$data.message);
  12. </script>
  13. </script>

Vue与MVVM的关系

MVVM可以分为四部分:
Model:模型;
View:视图;
ViewModel:视图模型;
Binder:绑定器;
vue与他们的对应关系:
视图对应的html与css部分;
模型对应的vue实例中的属性,如计算属性computed等;
视图模型对应的vue下的模板语法;
绑定器对应v-bind,v-model等绑定数据的语法;


Vue中的常用属性

计算属性computed:

计算属性会与data中的属性变量合并;如果将计算属性写入data中,那么这个属性变量在插值中渲染时要作为函数;例子({{函数名()}});
过滤器属性(filters)可以用在两个地方,双花括号插值和v-bind表达式;
实例展示:

  1. <div class="app">
  2. <p>{{reversed()}}</p>
  3. <p>{{casee()}}</p>
  4. </div>
  5. <script>
  6. const vm=new Vue({
  7. el:".app",
  8. data:{
  9. word:"admin",
  10. ju:"hello admin",
  11. reversed(){
  12. return this.word.split("").reverse().join("");
  13. },
  14. casee(){
  15. return this.ju.toUpperCase().substr(5);
  16. }
  17. },
  18. });
  19. </script>

效果展示:

侦听器属性

  1. vue的侦听器属性(watch)就相当于原生js中的事件监听器,负责监听数据变化时需要执行的操作;
  2. 使用侦听器属性时,需要有v-model指令的双向数据绑定,否则无法被监听;
    实例演示:
    1. <div class="app">
    2. <input type="number" v-model="add1" value="">+<input type="number" v-model="add2" value="">={{res}}
    3. </div>
    4. <script>
    5. const vm=new Vue({
    6. el:".app",
    7. data:{
    8. add1:0,
    9. add2:0,
    10. res:0,
    11. },
    12. methods:{
    13. add(x,y){
    14. this.res=x * 1 + y * 1;
    15. }
    16. },
    17. // 侦听器属性
    18. watch:{
    19. add1(str) {
    20. this.add(str, this.add2);
    21. },
    22. add2(str) {
    23. this.add(str, this.add1);
    24. },
    25. }
    26. })
    27. console.log(vm.add1)
    效果展示
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!