Android开发经验谈Android开发程序员

Android开发人员不得不学习的Vue.js基础

2018-08-21  本文已影响69人  24K纯帅豆

介绍

Vue是一套用于构建用户界面的渐进式框架。Vue 被设计为可以自底向上逐层应用。Vue 的核心库只关注视图层,不仅易于上手,还便于与第三方库或既有项目整合。另一方面,当与现代化的工具链以及各种支持类库结合使用时,Vue 也完全能够为复杂的单页应用提供驱动。

用法

1、Vue实例

<body>
  <div id="root">{{msg}}</div>

  <script>
    new Vue({
      el: "#root",
      data: {
        msg: "hello world"
      },
      methods{
      
      }
    })
  </script>
</body>

上述代码中,在script标签中实例化了一个Vue对象,该Vue对象和id为root的div标签绑定了,所以可以通过Vue来操作这个div标签。data中存放的数据可供div调用,这样可以动态的修改div中的数据;methods中用于存放各种方法,供标签调用。div标签又被称作挂载点;{{}}被称作插值表达式。

2、Vue中的数据、事件和方法

<body>
  <div id="root">
     <h1 v-text="number"></h1>
  </div>

  <script>
    new Vue({
      el: "#root",
      data: {
        msg: "hello world",
        number: "123"
      }
    })
  </script>
</body>
<body>
  <div id="root">
     <h1 v-text="number"></h1>
     <h1 v-html="content"></h1>
  </div>

  <script>
    new Vue({
      el: "#root",
      data: {
        msg: "hello world",
        number: "123",
        content: "<h1>hello world</h1>"
      }
    })
  </script>
</body>
<body>

  <div id="root">
    <div @click="handleClick">{{content}}</div>
  </div>

  <script>
    new Vue({
      el: "#root",
      data: {
        content: "hello"
      },
      methods: {
        handleClick: function() {
          this.content = "world"
        }
      }
    })
  </script>
</body>

3、属性绑定和双向数据绑定

<body>

  <div id="root">
    <div v-bind:title="title">{{content}}</div>
  </div>

  <script>
    new Vue({
      el: "#root",
      data: {
        content: "hello world",
        title:"I'm hello world"
      }
    })
  </script>
</body>
<body>

  <div id="root">
    <div v-bind:title="title">{{content}}</div>
    <input v-model="vcontent" />
    <div>{{vcontent}}</div>
  </div>

  <script>
    new Vue({
      el: "#root",
      data: {
        content: "hello world",
        title:"I'm hello world",
        vcontent:"I'm v-model"
      }
    })
  </script>
</body>

4、计算属性和侦听器

<body>

  <div id="root">
    <div v-bind:title="title">{{content}}</div>
    <input v-model="vcontent" />
    <div>{{vcontent}}</div>
    姓:<input v-model="firstName" /> 名:<input v-model="lastName" />
    <div>{{fullName}}</div>
  </div>

  <script>
    new Vue({
      el: "#root",
      data: {
        content: "hello world",
        title: "I'm hello world",
        vcontent: "I'm v-model",
        firstName: '',
        lastName: ''
      },
      computed :{
        fullName: function() {
          return this.firstName + ' ' + this.lastName
        }
      }
    })
  </script>
</body>
<body>

  <div id="root">
    <div v-bind:title="title">{{content}}</div>
    <input v-model="vcontent" />
    <div>{{vcontent}}</div>
    姓:<input v-model="firstName" /> 名:<input v-model="lastName" />
    <div>{{fullName}}</div>
    <div>{{count}}</div>
  </div>

  <script>
    new Vue({
      el: "#root",
      data: {
        content: "hello world",
        title: "I'm hello world",
        vcontent: "I'm v-model",
        firstName: '',
        lastName: '',
        count: 0
      },
      computed: {
        fullName: function() {
          return this.firstName + ' ' + this.lastName;
        }
      },
      watch: {
        fullName: function() {
          this.count++;
        }
      }
    })
  </script>
</body>

5、三个常见指令

<body>

  <div id="root">
    <div v-bind:title="title">{{content}}</div>
    <input v-model="vcontent" />
    <div>{{vcontent}}</div>
    姓:<input v-model="firstName" /> 名:<input v-model="lastName" />
    <div>{{fullName}}</div>
    <div v-if="show">{{count}}</div>
    <button @click="handleClick">toggon</button>
  </div>

  <script>
    new Vue({
      el: "#root",
      data: {
        content: "hello world",
        title: "I'm hello world",
        vcontent: "I'm v-model",
        firstName: '',
        lastName: '',
        count: 0,
        show:true
      },
      methods: {
        handleClick: function() {
          this.show=!this.show;
        }
      },
      computed: {
        fullName: function() {
          return this.firstName + ' ' + this.lastName;
        }
      },
      watch: {
        fullName: function() {
          this.count++;
        }
      }
    })
  </script>
</body>
<body>

  <div id="root">
    <div v-bind:title="title">{{content}}</div>
    <input v-model="vcontent" />
    <div>{{vcontent}}</div>
    姓:<input v-model="firstName" /> 名:<input v-model="lastName" />
    <div>{{fullName}}</div>
    <div v-if="show">{{count}}</div>
    <button @click="handleClick">toggon</button>
    <ul>
      <li v-for="(item, index) of list" :key="index">
        {{item}}
      </li>
    </ul>
  </div>

  <script>
    new Vue({
      el: "#root",
      data: {
        content: "hello world",
        title: "I'm hello world",
        vcontent: "I'm v-model",
        firstName: '',
        lastName: '',
        count: 0,
        show:true,
        list:[1,2,3]
      },
      methods: {
        handleClick: function() {
          this.show=!this.show;
        }
      },
      computed: {
        fullName: function() {
          return this.firstName + ' ' + this.lastName;
        }
      },
      watch: {
        fullName: function() {
          this.count++;
        }
      }
    })
  </script>
</body>

组件的使用

组件是可复用的Vue实例,且带有一个名字,在Vue中分为全局组件和局部组件,局部组件需要在Vue对象中注册才能被使用。

1、全局组件的定义

<body>
    <todo-item></todo-item>
</body>

<script>
    Vue.component('todo-item',{
        template: '<li>item</li>'
    })
    
    new Vue({
        
    })
</script>

2、局部组件的定义

<body>
    <todo-item></todo-item>
</body>

<script>
    var TodoItem={
        template='<li>item</li>';
    }
    
    new Vue({
        //注册局部组件,然后才能在外部使用
        components:{
            'todo-item':TodoItem
        }
    })
</script>
上一篇下一篇

猜你喜欢

热点阅读