Vue

【Vue】表单输入绑定与组件基础

2019-10-13  本文已影响0人  Qingelin

1. 表单输入绑定

<!-- 在“change”时而非“input”时更新 -->
<input v-model.lazy="msg" >
<input v-model.number="age" type="number">
<input v-model.trim="msg">

2. 组件

2.1 注册一个全局组件基础语法

Vue.component('button-counter', options)

这里的options必须满足以下要求:

  1. 因为组件是一个可复用的vue实例,所以它们与 new Vue 接收几乎相同的选项,例如 data、computed、watch、methods 以及生命周期钩子等。
  2. 组件不接受el
  3. 这里的 options.data 选项必须是一个函数

2.2 组件的组织

每一个vue组件都必须只有一个根元素

//每个 Vue 组件必须只有一个根元素,所以下面的组件是错的(控制台会出现一个警告)
Vue.component('xxx',{
  template:`
    hi
  `
})
//两个div必须有一个根元素
Vue.component('xxx',{
  template:`
    <div></div>
    <div></div>
  `
})

2.3 通过prop向子组件传递数据

//js
Vue.component('blog-post', {
  props: ['title'],
  template: '<h3>{{ title }}</h3>'
})
//html
<blog-post title="My journey with Vue"></blog-post>
<blog-post title="Blogging with Vue"></blog-post>
<blog-post title="Why Vue is so fun"></blog-post>

2.4 动态组件

<component v-bind:is="currentTabComponent"></component>

通过修改 currentTabComponent 的值,我们就可以让 component 在不同组件之间进行动态切换,这里的currentTabComponent可以包括:

  1. 已经注册组件的名字
  2. 一个组件的选项对象
上一篇 下一篇

猜你喜欢

热点阅读