加入收藏 | 设为首页 | 会员中心 | 我要投稿 焦作站长网 (https://www.0391zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 编程要点 > 语言 > 正文

vue 组件基础知识剖析

发布时间:2021-10-16 15:29:38 所属栏目:语言 来源:互联网
导读:这篇文章主要介绍了vue 组件基础知识的相关资料,帮助大家更好的理解和使用vue的组件,感兴趣的朋友可以了解下 ASP站长网 组件基础 1 组件的复用 组件是可复用的

组件是可复用的Vue实例。

 

<!DOCTYPE html>

<html>

 <head>

  <meta charset="utf-8"> 

  <style>

  

  </style>

  <script src=http://www.0792zz.cn/html/biancheng/yy/2021/1016/"https:/cdn.staticfile.org/vue/2.4.2/vue.min.js"></script>

 </head>

 <body>

  <div id="app">

   <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++">点击了 {{ count }} 次.</button>'

   });

 

   new Vue({ el: '#app' });

  </script>

 </body>

</html>

 

注意当点击按钮时,每个组件都会各自独立维护它的count。这里自定义组件的data属性必须是一个函数,每个实例维护一份被返回对象的独立的拷贝。

 

<!DOCTYPE html>

<html>

 <head>

  <meta charset="utf-8"> 

  <style>

  

  </style>

  <script src=http://www.0792zz.cn/html/biancheng/yy/2021/1016/"https:/cdn.staticfile.org/vue/2.4.2/vue.min.js"></script>

 </head>

 <body>

  <div id="app">

   <button-counter></button-counter>

   <button-counter></button-counter>

   <button-counter></button-counter>

  </div>

  <script>

   var buttonCounterData = {

    count: 0

   }

   // 定义一个名为 button-counter 的新组件

   Vue.component('button-counter', {

    data: function () {

     return buttonCounterData

    },

    template: '<button v-on:click="count++">点击了 {{ count }} 次.</button>'

   });

 

   new Vue({ el: '#app' });

  </script>

 </body>

</html>

 

2 通过 Prop 向子组件传递数据

 

<!DOCTYPE html>

<html>

 <head>

  <meta charset="utf-8"> 

  <style>

  

  </style>

  <script src=http://www.0792zz.cn/html/biancheng/yy/2021/1016/"https:/cdn.staticfile.org/vue/2.4.2/vue.min.js"></script>

 </head>

 <body>

  <div id="app">

   <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: '#app' });

  </script>

 </body>

</html>

 

这里组件就是通过自定义属性title来传递数据。

 

我们可以使用v-bind来动态传递prop。

 

<!DOCTYPE html>

<html>

 <head>

  <meta charset="utf-8"> 

  <style>

  

  </style>

  <script src=http://www.0792zz.cn/html/biancheng/yy/2021/1016/"https:/cdn.staticfile.org/vue/2.4.2/vue.min.js"></script>

 </head>

 <body>

  <div id="app">

   <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: '#app',

    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>

 

3 单个根元素

 

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

 

<!DOCTYPE html>

<html>

 <head>

  <meta charset="utf-8"> 

  <style>

  

  </style>

  <script src=http://www.0792zz.cn/html/biancheng/yy/2021/1016/"https:/cdn.staticfile.org/vue/2.4.2/vue.min.js"></script>

 </head>

 <body>

  <div id="app">

   <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: '#app',

    data: {

     posts: [

      { id: 1, title: 'My journey with Vue', content: 'my journey...' },

      { id: 2, title: 'Blogging with Vue', content: 'my blog...' },

      { id: 3, title: 'Why Vue is so fun', content: 'Vue is so fun...' }

     ]

    }

   });

  </script>

 </body>

</html>

 

注意到v-bind:post="post"绑定的post是一个对象,这样可以避免了需要通过很多prop传递数据的麻烦。

 

4 监听子组件事件

 

<!DOCTYPE html>

<html>

 <head>

  <meta charset="utf-8"> 

  <style>

  

  </style>

  <script src=http://www.0792zz.cn/html/biancheng/yy/2021/1016/"https:/cdn.staticfile.org/vue/2.4.2/vue.min.js"></script>

 </head>

 <body>

  <div id="app">

   <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" />

   </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')">放大字体</button>

      <div v-html="post.content"></div>

     </div>

    `

   })

 

   new Vue({

    el: '#app',

    data: {

     postFontSize: 1,

     posts: [

      { id: 1, title: 'My journey with Vue', content: 'my journey...' },

      { id: 2, title: 'Blogging with Vue', content: 'my blog...' },

      { id: 3, title: 'Why Vue is so fun', content: 'Vue is so fun...' }

     ]

    }

   });

  </script>

(编辑:焦作站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读