Vue.js 实战教程 V2.x(13)事件处理

 图片

13组件基础
13.1基本示例
Vue 组件的示例:
 
// 定义一个名为 button-counter 的新组件
 
Vue.component('button-counter', {
 
  data: function () {
 
    return {
 
      count: 0
 
    }
 
  },
 
  template: '<button v-on:click="count++">You clicked me {{ count }} times.</button>'
 
})
 
组件是可复用的 Vue 实例,且带有一个名字:在这个例子中是 <button-counter>。我们可以在一个通过 new Vue 创建的 Vue 根实例中,把这个组件作为自定义元素来使用:
 
<div id="components-demo">
 
  <button-counter></button-counter>
 
</div>
 
new Vue({ el: '#components-demo' })
 
13.2组件的复用
你可以将组件进行任意次数的复用:
 
<div id="components-demo">
 
  <button-counter></button-counter>
 
  <button-counter></button-counter>
 
  <button-counter></button-counter>
 
</div>
 
data 必须是一个函数
当我们定义这个 <button-counter> 组件时,你可能会发现它的 data 并不是像这样直接提供一个对象:
 
data: {
 
  count: 0
 
}
 
取而代之的是,一个组件的 data 选项必须是一个函数,因此每个实例可以维护一份被返回对象的独立的拷贝:
 
data: function () {
 
  return {
 
    count: 0
 
  }
 
}
 
如果 Vue 没有这条规则,点击一个按钮就可能会像如下代码一样影响到其它所有实例:
 
13.3组件的组织
通常一个应用会以一棵嵌套的组件树的形式来组织:
 
  图片
 
 
 
为了能在模板中使用,这些组件必须先注册以便 Vue 能够识别。这里有两种组件的注册类型:全局注册和局部注册。至此,我们的组件都只是通过 Vue.component 全局注册的:
 
Vue.component('my-component-name', {
 
  // ... options ...
 
})
 
全局注册的组件可以用在其被注册之后的任何 (通过 new Vue) 新创建的 Vue 根实例,也包括其组件树中的所有子组件的模板中。
 
13.4通过 Prop 向子组件传递数据
为了给博文组件传递一个标题,我们可以用一个 props 选项将其包含在该组件可接受的 prop 列表中:
 
Vue.component('blog-post', {
 
  props: ['title'],
 
  template: '<h3>{{ title }}</h3>'
 
})
 
一个组件默认可以拥有任意数量的 prop,任何值都可以传递给任何 prop。
 
一个 prop 被注册之后,你就可以像这样把数据作为一个自定义特性传递进来:
 
<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>
 
然而在一个典型的应用中,你可能在 data 里有一个博文的数组:
 
new Vue({
 
  el: '#blog-post-demo',
 
  data: {
 
    posts: [
 
      { id: 1, title: 'My journey with Vue' },
 
      { id: 2, title: 'Blogging with Vue' },
 
      { id: 3, title: 'Why Vue is so fun' }
 
    ]
 
  }
 
})
 
并想要为每篇博文渲染一个组件:
 
<blog-post
 
  v-for="post in posts"
 
  v-bind:key="post.id"
 
  v-bind:title="post.title"
 
></blog-post>
 
13.5单个根元素
当构建一个 <blog-post> 组件时,你的模板最终会包含的东西远不止一个标题:
 
<h3>{{ title }}</h3>
 
最最起码,你会包含这篇博文的正文:
 
<h3>{{ title }}</h3><div v-html="content"></div>
 
然而如果你在模板中尝试这样写,Vue 会显示一个错误,并解释道 every component must have a single root element (每个组件必须只有一个根元素)。你可以将模板的内容包裹在一个父元素内,来修复这个问题,例如:
 
<div class="blog-post">
 
  <h3>{{ title }}</h3>
 
  <div v-html="content"></div>
 
</div>
 
看起来当组件变得越来越复杂的时候,我们的博文不只需要标题和内容,还需要发布日期、评论等等。为每个相关的信息定义一个 prop 会变得很麻烦:
 
<blog-post
 
  v-for="post in posts"
 
  v-bind:key="post.id"
 
  v-bind:title="post.title"
 
  v-bind:content="post.content"
 
  v-bind:publishedAt="post.publishedAt"
 
  v-bind:comments="post.comments">
 
</blog-post>
 
所以是时候重构一下这个 <blog-post> 组件了,让它变成接受一个单独的 post prop:
 
<blog-post
 
  v-for="post in posts"
 
  v-bind:key="post.id"
 
  v-bind:post="post">
 
</blog-post>
 
Vue.component('blog-post', {
 
  props: ['post'],
 
  template: `
 
    <div class="blog-post">
 
      <h3>{{ post.title }}</h3>
 
      <div v-html="post.content"></div>
 
    </div>
 
  `
 
})
 
13.6监听子组件事件
在我们开发 <blog-post> 组件时,它的一些功能可能要求我们和父级组件进行沟通。例如我们可能会引入一个辅助功能来放大博文的字号,同时让页面的其它部分保持默认的字号。
 
在其父组件中,我们可以通过添加一个 postFontSize 数据属性来支持这个功能:
 
new Vue({
 
  el: '#blog-posts-events-demo',
 
  data: {
 
    posts: [/* ... */],
 
    postFontSize: 1
 
  }
 
})
 
它可以在模板中用来控制所有博文的字号:
 
<div id="blog-posts-events-demo">
 
  <div :style="{ fontSize: postFontSize + 'em' }">
 
    <blog-post
 
      v-for="post in posts"
 
      v-bind:key="post.id"
 
      v-bind:post="post"
 
    ></blog-post>
 
  </div>
 
</div>
 
现在我们在每篇博文正文之前添加一个按钮来放大字号:
 
Vue.component('blog-post', {
 
  props: ['post'],
 
  template: `
 
    <div class="blog-post">
 
      <h3>{{ post.title }}</h3>
 
      <button>
 
        Enlarge text
 
      </button>
 
      <div v-html="post.content"></div>
 
    </div>
 
  `
 
})
 
问题是这个按钮不会做任何事:
 
<button>
 
  Enlarge text
 
</button>
 
当点击这个按钮时,我们需要告诉父级组件放大所有博文的文本。幸好 Vue 实例提供了一个自定义事件的系统来解决这个问题。父级组件可以像处理 native DOM 事件一样通过 v-on 监听子组件实例的任意事件:
 
<blog-post
 
  ...
 
  v-on:enlarge-text="postFontSize += 0.1">
 
</blog-post>
 
同时子组件可以通过调用内建的 $emit 方法 并传入事件名称来触发一个事件:
 
<button v-on:click="$emit('enlarge-text')">
 
  Enlarge text
 
</button>
 
有了这个 v-on:enlarge-text="postFontSize += 0.1" 监听器,父级组件就会接收该事件并更新 postFontSize 的值。
 
使用事件抛出一个值
有的时候用一个事件来抛出一个特定的值是非常有用的。例如我们可能想让 <blog-post> 组件决定它的文本要放大多少。这时可以使用 $emit 的第二个参数来提供这个值:
 
<button v-on:click="$emit('enlarge-text', 0.1)">
 
  Enlarge text
 
</button>
 
然后当在父级组件监听这个事件的时候,我们可以通过 $event 访问到被抛出的这个值:
 
<blog-post
 
  ...
 
  v-on:enlarge-text="postFontSize += $event">
 
</blog-post>
 
或者,如果这个事件处理函数是一个方法:
 
<blog-post
 
  ...
 
  v-on:enlarge-text="onEnlargeText">
 
</blog-post>
 
那么这个值将会作为第一个参数传入这个方法:
 
methods: {
 
  onEnlargeText: function (enlargeAmount) {
 
    this.postFontSize += enlargeAmount
 
  }
 
}
 
在组件上使用 v-model
 
自定义事件也可以用于创建支持 v-model 的自定义输入组件。记住:
 
<input v-model="searchText">
 
等价于:
 
<input
 
  v-bind:value="searchText"
 
  v-on:input="searchText = $event.target.value"
 
>
 
当用在组件上时,v-model 则会这样:
 
<custom-input
 
  v-bind:value="searchText"
 
  v-on:input="searchText = $event">
 
</custom-input>
 
为了让它正常工作,这个组件内的 <input> 必须:
 
将其 value 特性绑定到一个名叫 value 的 prop 上
 
在其 input 事件被触发时,将新的值通过自定义的 input 事件抛出
 
写成代码之后是这样的:
 
Vue.component('custom-input', {
 
  props: ['value'],
 
  template: `
 
    <input
 
      v-bind:value="value"
 
      v-on:input="$emit('input', $event.target.value)"
 
    >
 
  `
 
})
 
现在 v-model 就应该可以在这个组件上完美地工作起来了:
 
<custom-input v-model="searchText"></custom-input>
 
13.7通过插槽分发内容
和 HTML 元素一样,我们经常需要向一个组件传递内容,像这样:
 
<alert-box>
 
  Something bad happened.
 
</alert-box>
 
幸好,Vue 自定义的 <slot> 元素让这变得非常简单:
 
Vue.component('alert-box', {
 
  template: `
 
    <div class="demo-alert-box">
 
      <strong>Error!</strong>
 
      <slot></slot>
 
    </div>
 
  `
 
})
 
13.8动态组件
有的时候,在不同组件之间进行动态切换是非常有用的,
 
可以通过 Vue 的 <component> 元素加一个特殊的 is 特性来实现:
 
<!-- 组件会在 `currentTabComponent` 改变时改变 -->
 
<component v-bind:is="currentTabComponent"></component>
 
13.9解析 DOM 模板时的注意事项
有些 HTML 元素,诸如 <ul>、<ol>、<table> 和 <select>,对于哪些元素可以出现在其内部是有严格限制的。而有些元素,诸如 <li>、<tr> 和 <option>,只能出现在其它某些特定的元素内部。
 
这会导致我们使用这些有约束条件的元素时遇到一些问题。例如:
 
<table>
 
  <blog-post-row></blog-post-row>
 
</table>
 
这个自定义组件 <blog-post-row> 会被作为无效的内容提升到外部,并导致最终渲染结果出错。幸好这个特殊的 is 特性给了我们一个变通的办法:
 
<table>
 
  <tr is="blog-post-row"></tr>
 
</table>
 
需要注意的是如果我们从以下来源使用模板的话,这条限制是不存在的:
 
字符串 (例如:template: '...')
单文件组件 (.vue)
<script type="text/x-template">
到这里,你需要了解的解析 DOM 模板时的注意事项——实际上也是 Vue 的全部必要内容,大概就是这些了。恭喜你!接下来还有很多东西要去学习,不过首先,我们推荐你先休息一下,试用一下 Vue,自己随意做些好玩的东西。
 
 
 
完整代码:
 
13 组件基础1.html
 
<!DOCTYPE html>
 
<html>
 
<head>
    
<title>组件基础</title>
    
 
</head>
 
<body>
<div id="components-demo">
  <button-counter></button-counter>
</div>
 
<script>
// 定义一个名为 button-counter 的新组件
Vue.component('button-counter', {
  data: function () {
    return {
      count: 0
    }
  },
  template: '<button v-on:click="count++">You clicked me {{ count }} times.</button>'
})
  
new Vue({ el: '#components-demo' })
 
</script>
 
</body>
 
</html>
 
 
13 组件基础2.html
 
<!DOCTYPE html>
 
<html>
 
<head>
    
<title>组件基础</title>
    
 
</head>
 
<body>
<div id="components-demo">
  <button-counter></button-counter>
  <button-counter></button-counter>
  <button-counter></button-counter>
</div>
 
<script>
// 定义一个名为 button-counter 的新组件
Vue.component('button-counter', {
  data: function () {
    return {
      count: 0
    }
  },
  template: '<button v-on:click="count++">You clicked me {{ count }} times.</button>'
})
  
new Vue({ el: '#components-demo' })
 
</script>
 
</body>
 
</html>
 
 
13 组件基础3.html
 
<!DOCTYPE html>
 
<html>
 
<head>
    
<title>组件基础</title>
    
 
</head>
 
<body>
<div id="blog-post-demo">
  <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>
 
</div>
 
<script>
Vue.component('blog-post', {
  props: ['title'],
  template: '<h3>{{ title }}</h3>'
})
  
new Vue({
  el: '#blog-post-demo',
  data: {
    
  }
})
 
 
</script>
 
</body>
 
</html>
 
 
13 组件基础4.html
 
<!DOCTYPE html>
 
<html>
 
<head>
    
<title>组件基础</title>
    
 
</head>
 
<body>
<div id="blog-post-demo">
  <blog-post
    v-for="post in posts"
    v-bind:key="post.id"
    v-bind:title="post.title">
  </blog-post>
 
</div>
 
<script>
Vue.component('blog-post', {
  props: ['title'],
  template: '<h3>{{ title }}</h3>'
})
  
new Vue({
  el: '#blog-post-demo',
  data: {
    posts: [
      { id: 1, title: 'My journey with Vue' },
      { id: 2, title: 'Blogging with Vue' },
      { id: 3, title: 'Why Vue is so fun' }
    ]
  }
})
 
</script>
 
</body>
 
</html>
 
 
13 组件基础5.html
 
<!DOCTYPE html>
 
<html>
 
<head>
    
<title>组件基础</title>
    
 
</head>
 
<body>
<div id="blog-post-demo">
  <blog-post
    v-for="post in posts"
    v-bind:key="post.id"
    v-bind:title="post.title"
v-bind:content="post.content"
  >
  </blog-post>
 
</div>
 
<script>
Vue.component('blog-post', {
  props: ['title', 'content'],
  template: `<div class="blog-post">
  <h3>{{ title }}</h3>
  <div v-html="content"></div>
</div>
`
})
  
new Vue({
  el: '#blog-post-demo',
  data: {
    posts: [
      { id: 1, title: 'My journey with Vue', content: 'My journey with Vue content content content' },
      { id: 2, title: 'Blogging with Vue', content: 'Blogging with Vue content content content' },
      { id: 3, title: 'Why Vue is so fun', content: 'Why Vue is so fun content content content' }
    ]
  }
})
 
</script>
 
</body>
 
</html>
 
 
13 组件基础6.html
 
<!DOCTYPE html>
 
<html>
 
<head>
    
<title>组件基础</title>
    
 
</head>
 
<body>
<div id="blog-post-demo">
  <blog-post
    v-for="post in posts"
    v-bind:key="post.id"
    v-bind:post="post">
  </blog-post>
 
</div>
 
<script>
Vue.component('blog-post', {
  props: ['post'],
  template: `
    <div class="blog-post">
      <h3>{{ post.title }}</h3>
      <div v-html="post.content"></div>
    </div>
  `
})
  
new Vue({
  el: '#blog-post-demo',
  data: {
    posts: [
      { id: 1, title: 'My journey with Vue', content: 'My journey with Vue content content content' },
      { id: 2, title: 'Blogging with Vue', content: 'Blogging with Vue content content content' },
      { id: 3, title: 'Why Vue is so fun', content: 'Why Vue is so fun content content content' }
    ]
  }
})
 
</script>
 
</body>
 
</html>
 
 
13 组件基础7.html
 
<!DOCTYPE html>
 
<html>
 
<head>
    
<title>组件基础</title>
    
 
</head>
 
<body>
<div id="blog-posts-events-demo">
  <div :style="{ fontSize: postFontSize + 'em' }">
    <blog-post
      v-for="post in posts"
      v-bind:key="post.id"
      v-bind:post="post"
  v-on:enlarge-text="postFontSize += 0.1"
    ></blog-post>
  </div>
</div>
 
<script>
Vue.component('blog-post', {
  props: ['post'],
  template: `
    <div class="blog-post">
      <h3>{{ post.title }}</h3>
      <button v-on:click="$emit('enlarge-text')">
        Enlarge text
      </button>
      <div v-html="post.content"></div>
    </div>
  `
})
  
new Vue({
  el: '#blog-posts-events-demo',
  data: {
    posts: [
      { id: 1, title: 'My journey with Vue', content: 'My journey with Vue content content content' },
      { id: 2, title: 'Blogging with Vue', content: 'Blogging with Vue content content content' },
      { id: 3, title: 'Why Vue is so fun', content: 'Why Vue is so fun content content content' }
    ],
postFontSize: 1
  }
})
 
</script>
 
</body>
 
</html>
 
 
13 组件基础8.html
 
<!DOCTYPE html>
 
<html>
 
<head>
    
<title>组件基础</title>
    
 
</head>
 
<body>
<div id="blog-posts-events-demo">
  <div :style="{ fontSize: postFontSize + 'em' }">
    <blog-post
      v-for="post in posts"
      v-bind:key="post.id"
      v-bind:post="post"
  v-on:enlarge-text="postFontSize += $event"
    ></blog-post>
  </div>
</div>
 
<script>
Vue.component('blog-post', {
  props: ['post'],
  template: `
    <div class="blog-post">
      <h3>{{ post.title }}</h3>
      <button v-on:click="$emit('enlarge-text', 0.1)">
        Enlarge text
      </button>
      <div v-html="post.content"></div>
    </div>
  `
})
  
new Vue({
  el: '#blog-posts-events-demo',
  data: {
    posts: [
      { id: 1, title: 'My journey with Vue', content: 'My journey with Vue content content content' },
      { id: 2, title: 'Blogging with Vue', content: 'Blogging with Vue content content content' },
      { id: 3, title: 'Why Vue is so fun', content: 'Why Vue is so fun content content content' }
    ],
postFontSize: 1
  }
})
 
</script>
 
</body>
 
</html>
 
 
13 组件基础9.html
 
<!DOCTYPE html>
 
<html>
 
<head>
    
<title>组件基础</title>
    
 
</head>
 
<body>
<div id="blog-posts-events-demo">
  <div :style="{ fontSize: postFontSize + 'em' }">
    <blog-post
      v-for="post in posts"
      v-bind:key="post.id"
      v-bind:post="post"
  v-on:enlarge-text="onEnlargeText"
    ></blog-post>
  </div>
</div>
 
<script>
Vue.component('blog-post', {
  props: ['post'],
  template: `
    <div class="blog-post">
      <h3>{{ post.title }}</h3>
      <button v-on:click="$emit('enlarge-text', 0.1)">
        Enlarge text
      </button>
      <div v-html="post.content"></div>
    </div>
  `
})
  
new Vue({
  el: '#blog-posts-events-demo',
  data: {
    posts: [
      { id: 1, title: 'My journey with Vue', content: 'My journey with Vue content content content' },
      { id: 2, title: 'Blogging with Vue', content: 'Blogging with Vue content content content' },
      { id: 3, title: 'Why Vue is so fun', content: 'Why Vue is so fun content content content' }
    ],
postFontSize: 1
  },
  methods: {
    onEnlargeText: function (enlargeAmount) {
      this.postFontSize += enlargeAmount
    }
  }
})
 
</script>
 
</body>
 
</html>
 
 
13 组件基础10.html
 
<!DOCTYPE html>
 
<html>
 
<head>
    
<title>组件基础</title>
    
 
</head>
 
<body>
<div id="alert-box">
  <alert-box>
    Something bad happened.
  </alert-box>
 
</div>
 
<script>
Vue.component('alert-box', {
  template: `
    <div class="demo-alert-box">
      <strong>Error!</strong>
      <slot></slot>
    </div>
  `
})
 
  
new Vue({
  el: '#alert-box',
  data: {
    
  },
  methods: {
    
  }
})
 
</script>
 
<style>
.demo-alert-box {
  color: red
}
</style>
</body>
 
</html>

欢迎观看视频教程:https://ke.qq.com/course/432961?tuin=36914f34,如有疑问,请加QQ群665714453交流讨论。
 

猜你喜欢

转载自www.cnblogs.com/daqiang123/p/11368421.html